簡體   English   中英

僅通過后退按鈕隱藏軟鍵盤

[英]Hide soft keyboard by back button only

我有一個帶有EditText的Fragment,當我單擊它時,軟鍵盤就會顯示出來,這是可以的。.現在,我想保持該鍵盤可見,無論如何,直到我按“返回”按鈕為止。 當前,每當我在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旁邊,我也希望保持軟輸入鍵盤可見,僅此而已。

按下后退按鈕時,可以使用此按鈕隱藏鍵盤:

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;
        }
});

希望能幫助到你 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM