Android:带EditText的AlertDialog不会自动显示键盘

时间:2011-08-22 13:09:53

标签: java android android-layout

我有一个连续两次显示的AlertDialog。在Nexus S上,一切都按照我的预期运行,但在Wildfire上键盘消失,当第二次显示Dialog时。

它必定是一些计时问题,因为键盘显示,当我在构造函数中放置一个断点并从那里继续。也许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() {
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

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

希望它可以帮到你!

答案 1 :(得分:0)

你可以试试这个,

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