仅通过后退按钮隐藏软键盘
我有一个带有EditText的碎片,当我点击它时,软键盘显示哪个是OK ..现在我想让这个键盘保持可见状态,直到我按下后退按钮。 目前,只要我在EditText外单击,键盘立即隐藏,我不想那样。
我的片段XML:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/conversation_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/myList"
android:layout_width="0dp"
android:layout_height="0dp"
android:divider="@null"
android:paddingBottom="2dp"
android:transcriptMode="alwaysScroll"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
tools:layout_editor_absoluteX="0dp" />
<LinearLayout
android:id="@+id/controlsLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteX="0dp">
.....
<EditText
android:id="@+id/messageInput"
android:layout_width="0dp"
android:layout_height="44dp"
android:inputType="textAutoComplete|textMultiLine"
android:paddingEnd="1dp"
android:paddingStart="3dp"
android:textColor="@android:color/black" />
.....
</LinearLayout>
注意:我不想拦截后退按钮。 即使我点击了EditText一侧,我也想让Soft Input Keyboard保持可见状态,就是这样。
当按下后退按钮时,您可以使用它来隐藏键盘:
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
要禁用焦点丢失时隐藏键盘的功能,我认为您可以在处理屏幕触摸的Fragment上添加一个侦听器,并且不执行任何操作:
View view = inflater.inflate(R.layout.fragment_test, container, false);
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Do nothing
return true;
}
});
希望能帮助到你 !
链接地址: http://www.djcxy.com/p/93441.html