簡體   English   中英

AlertDialog.Builder中EditText的自動鍵盤顯示

[英]Automatic keyboard display for EditText in AlertDialog.Builder

很抱歉提出以前似乎曾經被問過並回答過的問題,但我發現沒有一種解決方案似乎對我有用。 我正在創建一個帶有EditTextAlertDialog來從用戶那里獲取一個字符串。 當顯示此對話框時,沒有可見的軟鍵盤,並且僅當用戶點擊EditText時,鍵盤才會彈出。 如何使EditText自動具有焦點,鍵盤如何自動顯示對話框顯示的時間?

這是我用於創建和顯示對話框的代碼。 所有這些都在OnClickListener內部,用於主活動上的按鈕。

@Override
public void onClick(View v) {
    final EditText textFileName = new EditText(MainActivity.this);
    textFileName.setRawInputType(Configuration.KEYBOARD_QWERTY);
    textFileName.setInputType(InputType.TYPE_CLASS_TEXT);

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
    .setTitle("Save Data")
    .setMessage("Specify file name for saved data.")
    .setView(textFileName)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do the file saving bit here
        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do whatever you feel is important here
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}

現在,我確實搜索了該問題的解決方案,並找到了兩個答案, 一個在這里另一個在這里 這兩個似乎都滿足了各自的原始海報,但是它們都不適合我,我只是弄不清楚我做錯了什么。

這是我的首次嘗試,基於上面的代碼和第一個鏈接中發布的最高投票答案。

@Override
public void onClick(View v) {
    final EditText textFileName = new EditText(MainActivity.this);
    textFileName.setRawInputType(Configuration.KEYBOARD_QWERTY);
    textFileName.setInputType(InputType.TYPE_CLASS_TEXT);

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
    .setTitle("Save Data")
    .setMessage("Specify file name for saved data.")
    .setView(textFileName)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do the file saving bit here
        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do whatever you feel is important here
        }
    });
    AlertDialog dialog = builder.create();
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    dialog.show();
}

從本質上講 ,這只是在dialog.show()調用之前添加的一行代碼,但它沒有任何改變。 在我點擊EditText之前,對話框仍然會彈出並帶有最漂亮的EditText,但沒有鍵盤。

這是嘗試二,基於第二個鏈接中投票最高的答案。

@Override
public void onClick(View v) {
    final EditText textFileName = new EditText(MainActivity.this);
    textFileName.setRawInputType(Configuration.KEYBOARD_QWERTY);
    textFileName.setInputType(InputType.TYPE_CLASS_TEXT);
    textFileName.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            textFileName.post(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager inputMethodManager = (InputMethodManager)MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(textFileName, InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    });
    textFileName.requestFocus();

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
    .setTitle("Save Data")
    .setMessage("Specify file name for saved data.")
    .setView(textFileName)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do the file saving bit here
        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do whatever you feel is important here
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}

和以前一樣的故事。

有什么幫助嗎? 將不勝感激。

試試這個,對我有幫助:

editText.postDelayed(new Runnable() {
    @Override
    public void run() {
        InputMethodManager keyboard = (InputMethodManager) activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);
        keyboard.showSoftInput(editText, 0);
    }
}, 50);

暫無
暫無

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

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