【问题标题】:How to hide keyboard when dialog is open对话框打开时如何隐藏键盘
【发布时间】:2019-08-23 08:17:57
【问题描述】:

在其他情况下打开对话框时隐藏键盘方法不起作用。

我在堆栈上尝试了所有流行的 hideKeyboard 方法,但它们都不起作用。

fun hideKeyboard(activity: Activity) {
    if(activity == null) return
    val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm?.hideSoftInputFromWindow(activity.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}

我没有收到任何错误,但键盘不会关闭。

【问题讨论】:

  • 你的对话框有编辑文本吗?
  • 它有 2 个按钮和 2 个 AutoCompleteTextViews
  • 打开对话框时是否打开键盘,然后您可以尝试此处提供的解决方案stackoverflow.com/questions/17362273/…
  • 这不起作用...它说“未解析的引用 getWidow()”,如果我在前面添加这个 dialog.this.getWindow() 比它说“表达式不能是选择器”
  • 我希望你使用 AlertDialog 然后在下面查看我的答案

标签: android kotlin dialog keyboard


【解决方案1】:

试试这个

public static void hideKeyboardFrom(Context context, View view) 
{ 
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
}

对于科特林

fun hideKeyboard(context : Context, view : View) 
{ 
    val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager 
    imm.hideSoftInputFromWindow(view.windowToken, 0) 
}

【讨论】:

  • 我需要通过什么? “这个”和?
  • 上下文和对话框视图
【解决方案2】:

如果您使用的是 AlertDialog,那么您可以按如下方式执行此操作。你可以从alertDialog对象获取getWindow()

alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

【讨论】:

  • val mDialogView = LayoutInflater.from(this).inflate(R.layout.activity_profile_name_surname_dialog, null) val mBuilder = AlertDialog.Builder(this).setView(mDialogView).setTitle("更改姓名和姓氏") val mAlertDialog = mBuilder.show()
  • mDialogView.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);不工作它说“未解决的引用getWidow()”
  • 使用 mAlertDialog.getWindow()
  • 我不知道...我在上面找到了解决方案
【解决方案3】:
HideKeyboard function Android Code 

public void hideKeyboard(View view, Context context) {

       try {

        if (view != null) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    } catch (Exception Ex) {
    }
}

【讨论】:

    【解决方案4】:
    1. 首先,您将调用隐藏键盘方法。我已经显示了下面给出的隐藏键盘方法类型。如果您想使用类似的方法。
        public void hideKeyboard(View view, Context context) {
                try {
                    if (view != null) {
                        InputMethodManager imm = (InputMethodManager) 
                        context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    }
                } catch (Exception Ex) {
                }
            }
        
            public static void hideKeyboard(Activity activity) {
                InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
                View view = activity.getCurrentFocus();
                if (view == null) {
                    view = new View(activity);
                }
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
    
    1. 然后你会调用Dialog。

    结论首先调用 hide 方法,然后调用 Dialog.So 当调用 hide 方法然后隐藏键盘并在调用对话框后再次调用 Dialog 然后打开。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 2017-09-30
      相关资源
      最近更新 更多