【问题标题】:Hide soft keyboard by back button only仅通过后退按钮隐藏软键盘
【发布时间】:2017-11-02 15:19:39
【问题描述】:

我有一个带有 EditText 的片段,当我点击它时,软键盘会出现,这没关系.. 现在我想让这个键盘保持可见,直到我按下返回按钮。 目前,每当我在 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 之外单击,我也希望保持软输入键盘可见,仅此而已。

【问题讨论】:

标签: android android-softkeyboard


【解决方案1】:

当按下返回按钮时,您可以使用它来隐藏键盘:

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

希望对您有所帮助!

【讨论】:

  • 我正在使用片段...“OnTouchEvent”未在片段中定义
  • @Abuzaid 对不起,我的错!您可以使用 OnTouchListener。在我编辑的帖子中找到示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-25
  • 2012-04-30
  • 2011-12-22
相关资源
最近更新 更多