【问题标题】:Soft keyboard doesn't get hide programmatically in android软键盘不会在android中以编程方式隐藏
【发布时间】:2016-06-25 05:34:57
【问题描述】:

我是 android 的新手,正在开发警报对话框的演示,一旦单击警报中的一个按钮,我想关闭软键盘。我已经尝试过编程但键盘仍然打开,请你帮忙我这个问题, 代码

  public void Show_Dialog() {
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                SwipeActivity.this);
        LayoutInflater inflater = this.getLayoutInflater();
        final View layout = inflater.inflate(R.layout.add_albom_dialog, null);
        alertDialog.setView(layout);

        final InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        //android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

        alertDialog.setPositiveButton("Create",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        EditText txts = (EditText) layout
                                .findViewById(R.id.addAblum_edit);
                        hideSoftKeyboardDialogDismiss(SwipeActivity.this);
                        if(txts.getText().toString().trim().length() > 0) {
                            Add_album(txts.getText().toString());

                        } else {

                            AlertDialog alertDialog = new AlertDialog.Builder(SwipeActivity.this).create();
                            alertDialog.setTitle("Error");
                            alertDialog.setMessage("Name can't be emtpy");
                            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                                            inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);

                                        }
                                    });
                            alertDialog.show();

                        }
                        dialog.cancel(); // Your custom code
                    }
                });

        /* When negative (No/cancel) button is clicked */
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        hideSoftKeyboardDialogDismiss(SwipeActivity.this);
                        dialog.cancel();
                        //  finish();

                    }

                });
        alertDialog.show();
    }

【问题讨论】:

    标签: android android-alertdialog android-softkeyboard android-input-method


    【解决方案1】:

    试试这个:

    protected void hideSoftKeyboard(EditText mSearchView) {
        InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
    }
    

    【讨论】:

    • Rosu alin -Awesome,你是个好兄弟……我还有一些问题。你能帮忙解决一下吗?
    • 在 stackoverflow 上提出他们,在这里留下评论?我去看看
    • 好的,兄弟。我告诉你,但我不能发布问题,它说你可以每 90 分钟发布一次..:(
    • 嗨,我正在向您解释我的问题,我有一个 listView,我正在滚动更新它,但是当数据进入并添加到列表时,我的列表转到第一项,有什么想法吗?
    • 检查以免再次设置适配器。在adapter中新建一个函数,在已有的数据中添加数据,无需再次在list中设置adapter
    【解决方案2】:
    dialog.setOnDissmissListener(){
       void onDismiss(){
    
        inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
    
       }
    } 
    dialog.dismiss();
    

    【讨论】:

      【解决方案3】:

      试试下面的方法

      final InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
      
      final AlertDialog alertDialog = new AlertDialog.Builder(SwipeActivity.this).create();
                        alertDialog.setTitle("Error");
                        alertDialog.setMessage("Name can't be emtpy");
                        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
      
                                       inputManager.hideSoftInputFromInputMethod(alertDialog.getCurrentFocus().getWindowToken(), 0);
                                       dialog.dismiss();
      
                                }
                        });
                       alertDialog.show();
      

      使用您的 alertDailog 的当前焦点而不是您的活动

      【讨论】:

        【解决方案4】:

        其实肯定有延迟所以用这段代码

          public static void hideSoftKeyboardDialogDismiss(final Activity activity) {
            new Handler().postDelayed(new Runnable() {
        
                @Override
                public void run() {
                    activity.runOnUiThread(new Runnable() {
        
                        @Override
                        public void run() {
                         InputMethodManager inputMethodManager =  (InputMethodManager) activity
                          .getSystemService(Activity.INPUT_METHOD_SERVICE);
                         if (null != activity.getCurrentFocus()) {
                          inputMethodManager.hideSoftInputFromWindow(activity
                           .getCurrentFocus().getWindowToken(), 0);
                          }
                        }
                    });
                }
            }, 1);
        }
        

        【讨论】:

        • 嗨,谢谢 ashish,我必须打电话给它吗?但是在哪里。你能帮我多一点兄弟吗..:)
        • 当您通过 dialog.dismiss 隐藏对话框时
        • 在其按钮点击时。有两个按钮“创建”和“取消”。在两个按钮上我都将其关闭。
        • 请使用这个 dialog.dismiss(); hideSoftKeyboardDialogDismiss(SwipeActivity.this);
        • 如果仍然不起作用,请尝试使用其他延迟值,例如 100 代替 hideSoftKeyboardDialogDismiss 中的 1
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-06
        • 2022-12-14
        • 2012-05-18
        • 1970-01-01
        • 2011-02-17
        • 2011-02-04
        • 1970-01-01
        相关资源
        最近更新 更多