簡體   English   中英

Android:帶EditText的AlertDialog不會自動顯示鍵盤

[英]Android: AlertDialog with EditText does not automatically show the Keyboard

我有一個連續兩次顯示的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() {
    }
}

嘗試這個 :

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

希望它能幫到你!

你可以試試這個,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM