一般,打开一个Activity,其中有一个EditText的话,系统会自动弹出软键盘。
本例Activity中需要启动一个AlertDialog,这个对话框使用的是自定义布局,在这个对话框里有个EditText,可能是自定义布局的问题,导致对话框弹出时不能自动打开软键盘并定位焦点到文本框里。
以下是我在米4(android:4.4.4,MIUI:miui-kxdcnbh34.0)里测试成功代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
private void confirmPhoneGurdPswd(final String guardPswd) { // 1.创建弹出式对话框 final AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(HomeActivity.this); // 系统默认Dialog没有输入框 // 获取自定义的布局 View alertDialogView = View.inflate(HomeActivity.this, R.layout.自定义布局, null); // 2.密码框-EditText。alertDialogView.findViewById(R.id.自定义布局中的文本框) final EditText et_dialog_confirmphoneguardpswd = (EditText) alertDialogView.findViewById(R.id.et_dialog_confirmphoneguardpswd); // 确认按钮,确认验证密码 Button btn_dialog_resolve_confirmphoneguardpswd = (Button) alertDialogView.findViewById(R.id.btn_dialog_resolve_confirmphoneguardpswd); btn_dialog_resolve_confirmphoneguardpswd.setOnClickListener(new OnClickListener() { // 点击按钮处理 public void onClick(View v) { // 提取文本框中输入的文本密码 } }); // 取消按钮,不验证密码 Button btn_dialog_cancel_confirmphoneguardpswd = (Button) alertDialogView.findViewById(R.id.btn_dialog_cancel_confirmphoneguardpswd); btn_dialog_cancel_confirmphoneguardpswd.setOnClickListener(new OnClickListener() { // 点击按钮处理 public void onClick(View v) { // } }); AlertDialog tempDialog = alertDialog.create(); tempDialog.setView(alertDialogView, 0, 0, 0, 0); /** 3.自动弹出软键盘 **/ tempDialog.setOnShowListener(new OnShowListener() { public void onShow(DialogInterface dialog) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(et_dialog_confirmphoneguardpswd, InputMethodManager.SHOW_IMPLICIT); } }); tempDialog.show(); } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/671.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设