简体   繁体   English

仅通过后退按钮隐藏软键盘

[英]Hide soft keyboard by back button only

I have a Fragment with an EditText, when I click on it the soft keyboard shows up which is OK.. Now I want to keep this keyboard visible no matter what until I press the Back button. 我有一个带有EditText的Fragment,当我单击它时,软键盘就会显示出来,这是可以的。.现在,我想保持该键盘可见,无论如何,直到我按“返回”按钮为止。 Currently, whenever I click outside the EditText the keyboard hides immediately, I don't want that. 当前,每当我在EditText外部单击时,键盘都会立即隐藏,但我不希望这样。

My Fragment XML: 我的片段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>

Note: I don't want to intercept Back button. 注意:我不想拦截“后退”按钮。 I want to keep the Soft Input Keyboard visible even if I click out side the EditText, That's all. 即使单击EditText旁边,我也希望保持软输入键盘可见,仅此而已。

You can use this to hide the keyboard when the back button is pressed : 按下后退按钮时,可以使用此按钮隐藏键盘:

InputMethodManager inputManager = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                     InputMethodManager.HIDE_NOT_ALWAYS);

To disable the feature which hide the keyboard when focus is lost, I think that you can add a listener on your Fragment that handle the touch on the screen and do nothing : 要禁用失去焦点时隐藏键盘的功能,我认为您可以在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;
        }
});

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM