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

3
我有一个AlertDialog,连续显示两次。在Nexus S上一切正常,但在Wildfire上,当第二次显示对话框时,键盘会消失。
这可能是一个时间问题,因为当我在构造函数中设置断点并从那里继续时,键盘就会出现。也许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() {
    }
}

请参考以下链接:https://dev59.com/tHE95IYBdhLWcg3wOLQ1 - Alex Orlov
2个回答

0

试试这个:

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

希望能对你有所帮助!


0
你可以尝试这个:
editText.setFocusable(true);
 requestfocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接