【问题标题】:How to hide keypad when editText is in focus当editText处于焦点时如何隐藏键盘
【发布时间】:2015-05-13 09:36:46
【问题描述】:
我希望 editText 框可以点击,以便用户可以滚动浏览文本,但我不希望键盘出现。
我已经尝试了所有这些,但他们没有做我想要的:
在 XML 中:
android:editable="false"
android:windowSoftInputMode="stateAlwaysHidden"
android:inputType="none"
这会禁用它:
editText.setInputType(EditorInfo.TYPE_NULL);
这仅适用于 API 21 及更高版本:
editText.setShowSoftInputOnFocus(false);
【问题讨论】:
标签:
android
android-edittext
focus
android-keypad
【解决方案1】:
试试这个
<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】:
使用这些代码行
EditText editText= (EditText) findViewById(R.id.editText);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
【解决方案3】:
在触摸监听器上设置编辑文本。这将使编辑文本不可触摸,因为它被设置为返回 true。
EditTextHere.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
声明它将允许用户通过将其返回为 false 来触摸编辑文本。