对话框中的Android EditText

时间:2012-11-21 12:02:49

标签: android android-edittext

在我的应用程序中,我在其中有EditText对话框。我想获得editbox的keyboad automaticaly.i.e,当显示对话框时,应显示键盘。有没有办法在不触及EditText的情况下显示键盘。

我添加了对话框代码

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(APP_NAME);
    builder.setMessage(message);
    // Use an EditText view to get user input.
    final EditText inputDevice = new EditText(this);
    final EditText inputTester = new EditText(this);

    if (id == DLG_ID_TESTER) {
        inputTester.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
        // inputTester.setThreshold(1);
        inputTester.setSelectAllOnFocus(true);
        inputTester.requestFocus();
        inputTester.setId(TXT_ID);
        builder.setView(inputTester);
    } else {
        inputDevice.setId(TXT_ID);
        inputDevice.setSelectAllOnFocus(true);
        builder.setView(inputDevice);
    }
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int whichButton) {

            if (id == DLG_ID_TESTER) {
                String val = inputTester.getText().toString();
                testerName = val;
                txtTesterName.setText(testerName);
            } else if (id == DLG_ID_DEVICE) {
                String value = inputDevice.getText().toString();
                deviceId = value;
                txtDeviceId.setText(deviceId);

            }
            return;
        }

    });

    builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    return;

                }
            });

    return builder.create();
}

2 个答案:

答案 0 :(得分:1)

试试这个Link for Opening Dailog without touching Edittext

**YOUR_VIEW**.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager keyboard = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 
            }
        },200);

答案 1 :(得分:0)

您可以覆盖OnFocusChange()方法以查看整个对话框何时获得焦点,然后使用“始终可见”输入模式调用getWindow()。setSoftInputMode()。