我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
当前回答
只需在EditTect视图中添加此属性即可隐藏键盘。
android:focusable="false"
其他回答
一个简单的解决方法是只编辑Text.setEnabled(false);editText.setEnabled(true);在Button onClick()方法中。
只需在BaseActivity和BaseFragment中为整个应用程序创建通用方法
在onCreate()中初始化inputMethodManager
inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
使用此方法隐藏和显示键盘
public void hideKeyBoard(View view) {
if (view != null) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
public void showKeyboard(View view, boolean isForceToShow) {
if (isForceToShow)
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
else if (view != null)
inputMethodManager.showSoftInput(view, 0);
}
一些科特林代码:
从“活动”中隐藏键盘:
(currentFocus ?: View(this))
.apply { (getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager)
.hideSoftInputFromWindow(windowToken, 0) }
这对我很有用
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive())
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
如果您的应用程序以API Level 21或更高为目标,则可以使用默认方法:
editTextObj.setShowSoftInputOnFocus(false);
确保在EditText XML标记中设置了以下代码。
<EditText
....
android:enabled="true"
android:focusable="true" />