如何解决Android MaterialAutoCompleteTextView 在显示 ExposedDropdownMenu 时隐藏键盘
考虑使用 ExposedDowpdownMenu 嵌套在 TextInputLayout 中的 MaterialAutoCompleteTextView。如何隐藏先前 TextInputEditText 已显示的软键盘?如果我们使用 imeOption=actionNext 从先前聚焦的 TextInputEditText 导航,它也应该隐藏键盘。由于用 ExposedDropdownMenu 装饰的 MaterialAutoCompleteTextView 不可聚焦,因此 onFocusChanged() 不起作用。此外,我没有找到任何像“onShowDropDown”或类似的属性来触发我的“hideKeyboard()”方法。
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".75"
android:layout_marginStart="8dp"
android:hint="@string/cvinfo_city"
android:layout_marginBottom="@dimen/default_margin">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/cvinfo_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLines="1"
android:text="@={viewmodel.user.general.address.city}" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/cvinfo_country"
android:layout_marginBottom="@dimen/default_margin">
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="@+id/cvinfo_country"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionDone"
android:maxLines="1"
android:text="@{viewmodel.user.general.address.country}" />
</com.google.android.material.textfield.TextInputLayout>
解决方法
添加一个 OnFocusChangeListener 以在下拉菜单获得焦点时隐藏键盘:
dropdown.setOnFocusChangeListener { view,hasFocus ->
if (hasFocus) {
view.context.getSystemService(InputMethodManager::class.java).hideSoftInputFromWindow(view.windowToken,0)
}
}
,
要在打开下拉菜单时不显示键盘,请将以下内容添加到您的 XML 代码中:
android:inputType="none"
,
尝试使用此代码
//Step1: Add in the root of the layout
android:clickable="true"
android:focusableInTouchMode="true"
//Step2: Add this method
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}
//Step3: Add this setOnFocusChangeListener event in onStart for the editText
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v,boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。