简体   繁体   中英

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. Currently, whenever I click outside the EditText the keyboard hides immediately, I don't want that.

My Fragment 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.

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 :

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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