开发者

Android AlertDialog.Builder and softkeyboard

开发者 https://www.devze.com 2023-02-15 12:32 出处:网络
Is it possible to display automatically the soft-keyboard when the dialog with focused EditText created by AlertDialog.Builder is shown?

Is it possible to display automatically the soft-keyboard when the dialog with focused EditText created by AlertDialog.Builder is shown?

I have seen some some discussion about the topic, bu开发者_Python百科t I did not find any working solution.


There is more than one way to skin a mongoose.

AlertDialog.Builder builder = new AlertDialog.Builder(CurrentActivityName.this);
builder.setTitle(“Title”);
builder.setMessage(“Message”);

etc..

//This is the crucial part 
AlertDialog alertDlg = builder.create();
alertDlg.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

alertDlg.show();

It works good in both phone and tablet

Reference


Solved this

InputMethodManager imm = (InputMethodManager)
            SettingsActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);

    mDialog = mDialogBuilder.create();

    mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

        public void onDismiss(DialogInterface dialog) {
            dismissSoftKeyboard();
        }
    });

    mPinDialog.show();
    showSoftKeyboard();

}

private void showSoftKeyboard() {
    if (imm != null) {
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }
}

private void dismissSoftKeyboard() {
    if (imm != null) {
        imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号