我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
当前回答
您可以简单地将此代码添加到要隐藏软键盘的位置“
// Check if no view has focus:
View view = getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
其他回答
这对我所有奇怪的键盘行为都有效
private boolean isKeyboardVisible() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
mRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = mRootView.getRootView().getHeight() - (r.bottom - r.top);
return heightDiff > 100; // if more than 100 pixels, its probably a keyboard...
}
protected void showKeyboard() {
if (isKeyboardVisible())
return;
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (getCurrentFocus() == null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
} else {
View view = getCurrentFocus();
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
}
protected void hideKeyboard() {
if (!isKeyboardVisible())
return;
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
View view = getCurrentFocus();
if (view == null) {
if (inputMethodManager.isAcceptingText())
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
} else {
if (view instanceof EditText)
((EditText) view).setText(((EditText) view).getText().toString()); // reset edit text bug on some keyboards bug
inputMethodManager.hideSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
public void setKeyboardVisibility(boolean show) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(show){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}else{
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
}
}
事实上,Android权威机构总是在提供新的更新,但他们并没有处理所有Android开发人员在开发过程中面临的旧缺点。默认情况下,这应该由Android权威机构处理,在更改EditText的焦点时,应该隐藏/显示软输入键盘选项。但很抱歉,他们没有管理。好的,留下来。
以下是在“活动”或“片段”中显示/隐藏/切换键盘选项的解决方案。
显示视图的键盘:
/**
* open soft keyboard.
*
* @param context
* @param view
*/
public static void showKeyBoard(Context context, View view) {
try {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(view, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
显示带有活动上下文的键盘:
/**
* open soft keyboard.
*
* @param mActivity context
*/
public static void showKeyBoard(Activity mActivity) {
try {
View view = mActivity.getCurrentFocus();
if (view != null) {
InputMethodManager keyboard = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.SHOW_FORCED);
}
} catch (Exception e) {
e.printStackTrace();
}
}
显示带有片段上下文的键盘:
/**
* open soft keyboard.
*
* @param mFragment context
*/
public static void showKeyBoard(Fragment mFragment) {
try {
if (mFragment == null || mFragment.getActivity() == null) {
return;
}
View view = mFragment.getActivity().getCurrentFocus();
if (view != null) {
InputMethodManager keyboard = (InputMethodManager) mFragment.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.SHOW_FORCED);
}
} catch (Exception e) {
e.printStackTrace();
}
}
隐藏视图的键盘:
/**
* close soft keyboard.
*
* @param context
* @param view
*/
public static void hideKeyBoard(Context context, View view) {
try {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
使用“活动”上下文隐藏键盘:
/**
* close opened soft keyboard.
*
* @param mActivity context
*/
public static void hideSoftKeyboard(Activity mActivity) {
try {
View view = mActivity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
使用片段上下文隐藏键盘:
/**
* close opened soft keyboard.
*
* @param mFragment context
*/
public static void hideSoftKeyboard(Fragment mFragment) {
try {
if (mFragment == null || mFragment.getActivity() == null) {
return;
}
View view = mFragment.getActivity().getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) mFragment.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
切换键盘:
/**
* toggle soft keyboard.
*
* @param context
*/
public static void toggleSoftKeyboard(Context context) {
try {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
Meier的解决方案也适用于我。在我的例子中,我的应用程序的顶层是一个选项卡宿主,我想在切换选项卡时隐藏关键字-我从选项卡宿主视图获取窗口令牌。
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);
}
}
如果您需要在片段中隐藏键盘,则此方法有效。
public static void hideSoftKeyboard(Context context, View view) {
if (context != null && view != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
对于视图,只需传入
getView() method