簡體   English   中英

Android AlertDialog 中 EditText 的默認焦點和鍵盤

[英]Default Focus and Keyboard to EditText in Android AlertDialog

我在 Android 中使用 AlertDialog.Builder 來快速提示用戶輸入文本。 該對話框顯示並運行良好,但用戶必須單擊 EditText 字段以加載軟鍵盤。 有什么方法可以打開鍵盤並將焦點放在我的對話框打開時? 這是我的代碼:

final Map<String,Object> rowData = itemList.get(mPosition);
                    final EditText input = new EditText(searchList.getContext());
                input.requestFocus();


                input.setSingleLine();
                final AlertDialog dialog = new AlertDialog.Builder(searchList.getContext())
                .setTitle(StringUtils.getSafeString(rowData.get("label")))
                .setView(input)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) { 
                        rowData.put("value", StringUtils.getSafeString(input.getText()));
                        searchList.invalidateViews();

                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Do nothing.
                    }
                }).create();
                dialog.show();

使用以下代碼。 它對我有用。

    editText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            editText.post(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager inputMethodManager= (InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    });
    editText.requestFocus();

以編程方式將焦點設置在 Android 對話框中的 EditText 上時隱藏鍵盤。

我也有這個問題,這是一個非常簡單的修復 - 這是我建議的解決方案。 盡管它對我來說適用於 DialogFragments,但我認為沒有理由在您的情況下它不起作用。

基本上不會觸發軟鍵盤,因為視圖是以編程方式創建的。 實際的修復只是將這一行放在 onCreateDialog 方法中:

dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

來自DialogFragments上的 Android 文檔:

如果用戶專注於 EditText,軟鍵盤會自動出現。 為了通過我們的編程重點強制執行此操作,我們調用 getDialog().getWindow().setSoftInputMode()。 請注意,您之前可能在 Dialog 中完成的許多 Window 操作仍然可以在 DialogFragment 中完成,但您必須調用 getDialog().getWindow() 而不僅僅是 getWindow()。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    //setup your dialog builder and inflate the view you want here
    ...
    //Make sure your EditText has the focus when the dialog is displayed
    edit.requestFocus();
    //Create the dialog and save to a variable, so we can set the keyboard state
    Dialog dialog = builder.create();
    //now, set to show the keyboard automatically
    dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    return dialog;
}

在您的 XML 布局中

稱呼

<requestFocus/>

在您的默認 EditText 中

<EditText 
android:blabla
.... >
<requestFocus/>
</EditText>

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 隱藏鍵盤使用:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(),0);

或嘗試以下代碼,但您必須設置 requestFocus() 或您的編輯文本

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});

如果您需要更頻繁地使用自定義視圖

public class RequestFocusEditText extends AppCompatEditText {
    private RequestFocusEditText self;

    public RequestFocusEditText(final Context context, AttributeSet attrs) {
        super(context, attrs);
        this.self = this;

        setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                post(new Runnable() {
                    @Override
                    public void run() {
                    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    });
    requestFocus();
    }
}

暫無
暫無

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

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