如何在editText聚焦时隐藏键盘

时间:2023-02-13 18:27:04

I want the editText box to be click-able so the user can scroll through the text, but I do not want the keyboard to appear. I have tried all these but they do not do what I want:

我希望editText框可单击,以便用户可以滚动文本,但我不希望键盘出现。我尝试了所有这些,但他们没有做我想要的:

In XML:

android:editable="false"
android:windowSoftInputMode="stateAlwaysHidden"
android:inputType="none"

This disables it:

这会禁用它:

editText.setInputType(EditorInfo.TYPE_NULL); 

This only works on API 21 and above:

这仅适用于API 21及更高版本:

editText.setShowSoftInputOnFocus(false);

3 个解决方案

#1


Try this

<EditText
        android:id="@+id/et_file_url"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"
        android:lines="1"
        android:textIsSelectable="true" />

#2


Use these lines of code

使用这些代码行

  EditText editText= (EditText) findViewById(R.id.editText);
  InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

#3


Set an edit text on touch listener. This will make the edit text untouchable because it is set to return true.

在触摸侦听器上设置编辑文本。这将使编辑文本无法触及,因为它被设置为返回true。

   EditTextHere.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
          return true;
        }
    });

Make a statement that it will allow the user to touch edit text by returning it to false.

声明它允许用户通过将其返回false来触摸编辑文本。

#1


Try this

<EditText
        android:id="@+id/et_file_url"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"
        android:lines="1"
        android:textIsSelectable="true" />

#2


Use these lines of code

使用这些代码行

  EditText editText= (EditText) findViewById(R.id.editText);
  InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

#3


Set an edit text on touch listener. This will make the edit text untouchable because it is set to return true.

在触摸侦听器上设置编辑文本。这将使编辑文本无法触及,因为它被设置为返回true。

   EditTextHere.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
          return true;
        }
    });

Make a statement that it will allow the user to touch edit text by returning it to false.

声明它允许用户通过将其返回false来触摸编辑文本。