[英]How to close Android Soft KeyBoard programmatically?
我目前正在使用以下代碼顯示軟鍵盤
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
在這里我不會將軟鍵盤與 Edittext 綁定,因為我使用了上面的代碼。
現在我想關閉 SoftKeyboard,所以我目前正在使用下面的代碼,但它不起作用。
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
誰能建議我用什么來關閉軟鍵盤?
基於下面的答案,我想讓您清楚我沒有使用 EditText,我使用了我想在其上顯示鍵盤和隱藏鍵盤的布局。 我想將鍵盤按鍵事件發送到我沒有使用 editText 的遠程區域 bcoz。
我已經測試過,這是有效的:
...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
順便說一下,你的代碼的第二個參數不對,請看這里。
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);
使用此工作代碼:
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
如果需要,您可以使用整個類並在任何地方調用 KeyboardUtil.hideKeyBoard(context) 方法:
public class KeyboardUtil
{
public static void hideKeyboard(Activity activity)
{
try
{
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
catch (Exception e)
{
// Ignore exceptions if any
Log.e("KeyBoardUtil", e.toString(), e);
}
}
}
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
it's working for me i hope it's work for you..
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
user942821 隱藏它的答案有效:
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
但這也適用於我隱藏它:
imm.toggleSoftInput(0, 0);
您可能還想嘗試:
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
在第一個參數中使用“0”時,有時鍵盤會在我無法弄清楚如何復制的奇怪情況下在錯誤的位置切換。 我仍在測試最后一個示例,但會在我發現更多信息時更新。
有關更多信息,請參閱toggleSoftInput 文檔頁面。
這工作正常
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);
這是解決方案,它檢查鍵盤是否可見
public static void hideKeyboard(Activity activity) {
if (isKeyboardVisible(activity)) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
public static boolean isKeyboardVisible(Activity activity) {
///This method is based on the one described at http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device
Rect r = new Rect();
View contentView = activity.findViewById(android.R.id.content);
contentView.getWindowVisibleDisplayFrame(r);
int screenHeight = contentView.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
return
(keypadHeight > screenHeight * 0.15);
}
InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
為了隱藏鍵盤,
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
這里,“mView”可以是屏幕上可見的任何視圖
此代碼從AutoCompleteTextView
onItemClick
內部隱藏鍵盤
public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
// whatever your code does
InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
}
// Simple, reuseable call anywhere in code
myView.hideKeyboard()
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
fun View.showKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}
簡化添加Done
/ Next
... 操作到 edittext:閱讀這篇文章
刪除永遠使用getSystemService
要求: Splitties 庫
public void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
並在你想要的地方打電話
hideKeyboard(MainActivity.this);
你也可以試試
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
private void close() {
this.requestHideSelf(0);
}
這個方法很簡單
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.