5

我是新手到android和工作的警報演示對話框,我想關閉軟鍵盤一旦從警報按鈕之一clicked我已經嘗試過programaticaly但鍵盤仍然是開放的,你能不能請幫助我這個問題, 代碼軟鍵盤沒有得到隱藏編程方式在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(); 
    } 

回答

3

試試這個:

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

Rosu alin-令人敬畏,你是偉大的兄弟...我有更多的問題,你可以幫助嗎? –

+0

在stackoverflow上提高他們,讓他們在這裏留言?我會看看 –

+0

好吧,兄弟。我告訴你,但我不能發佈問題,說你可以每90分鐘發佈一次.. :( –

1

試着做以下方式

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目前的焦點牛逼的活動

+0

它讓我不能在兩行:( –

+0

解決eroor @ sulphuricAcid我更新了我的答案。現在可以試試嗎? –

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

嗨,感謝ashish,我必須打電話給它?但在哪裏可以幫助我多一點兄弟.. :) –

+0

當你隱藏對話框dialog.dismiss –

+0

在其按鈕clicks.there有兩個按鈕「創建」和「cancle」。在這兩個按鈕上,我都在dis it它。 –

2
dialog.setOnDissmissListener(){ 
    void onDismiss(){ 

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

    } 
} 
dialog.dismiss();