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

时间:2016-03-10 11:36:39

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

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

4 个答案:

答案 0 :(得分:4)

试试这个:

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

答案 1 :(得分:2)

a = [1,3,4,5]
b = [1,2,3]
(a - b).size < a.size
# => true

答案 2 :(得分:1)

尝试按以下方式执行

{
  "mediaServer" : {
    "resources": {
    //  //Resources usage limit for raising an exception when an object creation is attempted
    //  "exceptionLimit": "0.8",
    //  // Resources usage limit for restarting the server when no objects are alive
    //  "killLimit": "0.7",
        // Garbage collector period in seconds
        "garbageCollectorPeriod": 240
    },
    "net" : {
      "websocket": {
        "port": 8888,
        //"secure": {
        //  "port": 8433,
        //  "certificate": "defaultCertificate.pem",
        //  "password": ""
        //},
        //"registrar": {
        //  "address": "ws://localhost:9090",
        //  "localAddress": "localhost"
        //},
        "path": "kurento",
        "threads": 10
      }
    }
  }
}

使用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(); 的当前焦点而不是您的活动

答案 3 :(得分:1)

实际上必须有延迟才能使用此代码

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