【发布时间】:2020-02-12 06:34:33
【问题描述】:
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.LAT_LNG, Place.Field.ADDRESS, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = null;
if (initialQuery != null && !initialQuery.isEmpty()) {
intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields).
setInitialQuery(initialQuery).
setTypeFilter(TypeFilter.CITIES).
build(activity);
} else {
intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields).
setTypeFilter(TypeFilter.CITIES).
build(activity);
}
activity.startActivityForResult(intent, GOOGLE_PLACES_PICKER_REQUEST_CODE);
以上代码打开GooglePlacesPicker,我们可以在其中搜索地点。点击后退箭头应关闭键盘。但事实并非如此。
我尝试了以下方法来关闭键盘
public static void hide(Context context) {
try {
if (context != null) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (((Activity) context).getCurrentFocus() != null) {
View currentFocus = ((Activity) context).getCurrentFocus();
if (currentFocus.getWindowToken() != null) {
imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.SHOW_FORCED);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
但没有奏效。请帮忙
【问题讨论】:
-
你把隐藏软键盘的代码块放在哪里了?
onActivityResult? -
@GianhTran 是的
-
错了,
onActivityResult在用户点击返回按钮时不会被调用 -
@GianhTran 我也在
onResume()上尝试过,但没有成功。你有什么建议?
标签: android google-places-api android-softkeyboard googleplacesautocomplete google-place-picker