简体   繁体   English

软键盘没有在android中以编程方式隐藏

[英]Soft keyboard doesn't get hide programmatically in android

I am newbie to android and working on a demo for alert dialog box,I want to close the soft keyboard once one of the buttons from the alert is clicked.I have tried it programaticaly but keyboard remains open,can you pls help me for this issue, code 我是android的新手并且正在进行警报对话框的演示,我想在点击警报中的一个按钮后关闭软键盘。我已经尝试了programaticaly但是键盘仍然打开,你能不能帮我这个问题, 代码

  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();
    }

Try this: 试试这个:

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

    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);

   }
} 
dialog.dismiss();

Try doing it the following way 尝试以下方式

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();

Use your alertDailog 's current focus not your activity 使用alertDailog的当前焦点而不是您的活动

Actually there must be delay so use this code 实际上必须有延迟,所以使用此代码

  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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM