Google 最近更新了他们的 Places SDK for android,所以现在我也在更新我的代码。我正在尝试使用AutocompleteSupportFragment
允许用户设置他们的地址。
这是我的代码:
mAddressEditText = (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.address);
mAddressEditText.setPlaceFields(Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG));
mAddressEditText.setHint("Address");
mAddressEditText.setText("Test1"); // Works fine at the beginning, disappears after selecting a place and shows only the hint
mAddressEditText.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Log.d(TAG, "Place Selected");
// Other Stuff
mAddressEditText.setText("Test2"); // Doesn't Work, all I can see is the hint
mAddressEditText.setText(place.getAddress()); // Doesn't Work, all I can see is the hint
}
@Override
public void onError(Status status) {
Log.e(TAG, "An error occurred: " + status);
invalidAddressDialog.show();
}
});
在之前的 SDK 中,fragment 会自动将文本设置为选定的地址。这在新的 SDK 中不起作用(不确定这是否是故意的)。所以我试图手动设置它。正如您在我的代码中的注释中看到的那样,setText
在侦听器之外使用效果很好。在听众内部,他们没有。
我做错了什么还是这是一个错误?
编辑:这么久了,我仍然无法正确解决这个问题。非常清楚,我可以从片段中正确获取地址,唯一不起作用的是setText
.
但是,由于一些答案表明他们没有遇到同样的问题,我开始认为这可能与我正在使用的库版本有关?
这些是我的库build.gradle
:
api 'com.android.support:appcompat-v7:28.0.0'
api 'com.android.support:support-annotations:28.0.0'
api 'com.android.support:multidex:1.0.3'
api 'com.google.firebase:firebase-core:16.0.8'
api 'com.google.firebase:firebase-auth:16.2.1'
api 'com.google.firebase:firebase-firestore:18.2.0'
api 'com.google.firebase:firebase-storage:16.1.0'
api 'com.google.android.libraries.places:places:1.1.0'