我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
当前回答
我正在使用以下Kotlin Activity扩展:
/**
* Hides soft keyboard if is open.
*/
fun Activity.hideKeyboard() {
currentFocus?.windowToken?.let {
(getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager?)
?.hideSoftInputFromWindow(it, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
/**
* Shows soft keyboard and request focus to given view.
*/
fun Activity.showKeyboard(view: View) {
view.requestFocus()
currentFocus?.windowToken?.let {
(getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager?)
?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}
其他回答
public final class UtilsKeyboard {
/**
* Sets the cursor at the end of this edit text and shows a keyboard
*/
public static void focusKeyboard(@NonNull EditText editText) {
editText.requestFocus();
editText.setSelection(editText.getText().length());
showKeyboard(editText);
}
private static void showKeyboard(@NonNull View view) {
final InputMethodManager manager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (manager != null) {
manager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
}
public static boolean hideKeyboard(@NonNull Window window) {
View view = window.getCurrentFocus();
return hideKeyboard(window, view);
}
private static boolean hideKeyboard(@NonNull Window window, @Nullable View view) {
if (view == null) {
return false;
}
InputMethodManager inputMethodManager = (InputMethodManager) window.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
return inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
return false;
}
public static boolean hideKeyboard(@Nullable View view) {
if (view == null) {
return false;
}
InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
return inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
return false;
}
/**
* This will hide the keyboard and unfocus any focused view.
*/
public static void unfocusKeyboard(@NonNull Window window) {
// When showing the bottom menu, make sure the keyboard isn't and that no view has focus
hideKeyboard(window);
final View focused = window.getCurrentFocus();
if (focused != null) focused.clearFocus();
}}
您也可以使用此代码段在Kotlin中隐藏键盘。
fun hideKeyboard(activity: Activity?) {
val inputManager: InputMethodManager? =
activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as?
InputMethodManager
// check if no view has focus:
val v = activity?.currentFocus ?: return
inputManager?.hideSoftInputFromWindow(v.windowToken, 0)
}
碎片形式的科特林溶液:
fun hideSoftKeyboard() {
val view = activity?.currentFocus
view?.let { v ->
val imm =
activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager // or context
imm.hideSoftInputFromWindow(v.windowToken, 0)
}
}
检查您的清单是否没有与活动关联的此参数:
android:windowSoftInputMode="stateAlwaysHidden"
Kotlin中的Wiki答案:
1-在文件中创建顶级函数(例如,包含所有顶级函数的文件):
fun Activity.hideKeyboard(){
val imm = this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
var view = currentFocus
if (view == null) { view = View(this) }
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
2-然后在你需要的任何活动中调用它:
this.hideKeyboard()
Kotlin版本
val imm: InputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
//Hide:
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
//Show
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);