3

I have an AlertDialog that is shown two times in a row. On a Nexus S everything works as I expect it, but on a Wildfire the Keyboard disappears, when the Dialog is shown for the second time.

It must be some timing problem, because the keyboard is shown, when I put a breakpoint in the constructor and continue from there. Maybe onFocusChange is not the right place to make sure the keyboard should be shown.

How can I fix it? What would you look for in order to find the reason behind this problem?

/**
 * 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() {
    }
}
Ulrich Scheller
  • 12,492
  • 6
  • 26
  • 32
  • Refer to http://stackoverflow.com/questions/2403632/android-show-soft-keyboard-automatically-when-focus-is-on-an-edittext – Alex Orlov Aug 22 '11 at 13:35

2 Answers2

0

Try this :

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

Hope it helps you!

Anass
  • 5,682
  • 6
  • 25
  • 35
0

You might try this,

editText.setFocusable(true);
 requestfocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Andro Selva
  • 51,960
  • 51
  • 189
  • 237