2011-08-22 57 views
3

我有一個AlertDialog連續顯示兩次。在Nexus S上,所有功能都可以按照我的預期進行操作,但在Wildfire中,鍵盤會消失,當第二次顯示對話框時。Android:AlertDialog與EditText不會自動顯示鍵盤

它必須是一些計時問題,因爲鍵盤顯示,當我在構造函數中放置斷點並從那裏繼續。也許onFocusChange不是確保顯示鍵盤的正確位置。

我該如何解決?你會尋找什麼來找到這個問題背後的原因?

/** 
* Show a dialog asking the user to confirm the PIN. 
*/ 
private static abstract class PinConfirmationDialog extends AlertDialog { 

    protected PinConfirmationDialog(Context context, final int titleResource) { 
     super(context); 
     setTitle(titleResource); 

     // Set an EditText view to get user input 
     final EditText input = new EditText(context); 
     InputFilter[] FilterArray = new InputFilter[1]; 
     FilterArray[0] = new InputFilter.LengthFilter(4); 
     input.setFilters(FilterArray); 
     input.setKeyListener(DigitsKeyListener.getInstance(false,true)); 
     input.setInputType(InputType.TYPE_CLASS_NUMBER 
       | 16 /*InputType.TYPE_NUMBER_VARIATION_PASSWORD Since: API Level 11*/); 
     input.setTransformationMethod(new PasswordTransformationMethod()); 
     setView(input); 

     setButton(context.getString(R.string.okay_action), new OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       onOkButtonClicked(input.getText().toString()); 
      } 
     }); 
     setButton2(context.getString(R.string.cancel_action), new OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       onCancelButtonClicked(); 
      } 
     }); 

     input.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if (hasFocus) { 
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
       } 
      } 
     }); 
    } 

    /** 
    * OK Button was pressed 
    * @param pinCode The code the user has entered 
    */ 
    abstract void onOkButtonClicked(String pinCode); 

    /** 
    * Override method if needed 
    */ 
    protected void onCancelButtonClicked() { 
    } 
} 
+0

參考http://stackoverflow.com/questions/2403632/android-show-soft-keyboard-automatically-when-focus-is-on-an-edittext –

回答

0

試試這個:

// Setting of the Keyboard 
InputMethodManager imm = (InputMethodManager) 
getSystemService(Context.INPUT_METHOD_SERVICE); 
// For SHOW_FORCED 
imm.showSoftInput (YOUEDITTEXT, InputMethodManager.SHOW_FORCED); 

希望它可以幫助你!

+0

不幸的是沒有效果 –

0

你可以試試這個,

editText.setFocusable(true); 
requestfocus(); 
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);