android - Android隐藏键盘自动完成
问题描述
当我选择一个自动完成结果时,我试图隐藏键盘。这是我的自动完成代码。
input_address.setFocusable(false);
input_address.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Place.Field> fieldList = Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.NAME);
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fieldList).build(MainActivity.this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AUTOCOMPLETE_REQUEST_CODE && resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
input_address.setText(place.getName());
latitude = place.getLatLng().latitude;
longitude = place.getLatLng().longitude;
departure = "\"" + latitude + "\"" + ":" + "\"" + longitude + "\"";
}
if (resultCode == AutocompleteActivity.RESULT_ERROR) {
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(getApplicationContext(), status.getStatusMessage(),
Toast.LENGTH_SHORT).show();
}
}
这是我的 input_address 的 xml 代码
<EditText
android:id="@+id/input_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:hint="departure"
android:textSize="20sp"
android:inputType="text"
android:imeOptions="actionDone"
android:onClick="startAutocompleteActivity"
/>
当我点击自动完成结果时,我应该如何将我的代码更改为有键盘?我尝试使用 IMM 在 OnActivityResult 下添加,但没有成功。请帮忙谢谢。
解决方案
input_address.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Place.Field> fieldList = Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.NAME);
//Code for hiding keyboard
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fieldList).build(MainActivity.this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
}
});
推荐阅读
- powershell - Powershell Stdout 未在 Jenkins 中返回
- python - 如何在 Python asyncio 中将新的协同程序添加到已经运行的循环中?
- apache-nifi - Nifi 将 Azure Blob account_name/account_key 存储在一个地方
- sql - 展平重复行 - Oracle SQL
- excel - 不使用选择自动填充
- php - 为什么某些音频文件无法在 Twilio Play 中播放?
- ssl - 客户端无法通过 SSL 连接到 Apache Artemis
- class - 返回 null 的类列表中的字符串
- c# - VS2015 中的所有代码文件都被检出
- reactjs - 根据道具值显示组件反应