我有一个活动,可以使用户选择输入其地址详细信息(地址文本,建筑物/楼层/公寓号)。
我想访问/保存用户在AutocompleteSupportFragment中写的内容,尤其是在Places Autocomplete API未找到任何结果的情况下,因此我可以使用用户直接输入的内容。
到目前为止我尝试过的是: 1-不调用AutocompleteSupportFragment.setOnPlaceSelectedListener的onError方法。 2-将TextWatcher添加到AutocompleteSupportFragment.a(其中a是对EditText的引用)以在afterTextChanged中获取数据,但不会捕获任何数据。
XML代码:
<fragment
android:id="@+id/address_autocomplete_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java代码:
Places.initialize(getApplicationContext(), Constants.PLACES_API_KEY);
// Initialize the AutocompleteSupportFragment.
autocompleteFragment = (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.address_autocomplete_fragment);
// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS));
autocompleteFragment.setCountry("EG");
autocompleteFragment.a.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "User text: " + s);
}
});
// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
//Get info about the selected place.
Log.d(TAG, "Place: " + place.getName() + ", " + place.getId());
}
@Override
public void onError(Status status) {
//Handle the error.
Log.d(TAG, "An error occurred: " + status);
}
});