Android 在原生AlertDialog里添加自定義EditText

因為無UI設(shè)計圖,所以不需要花里胡哨的dialog,怎麼簡單怎么來。

需求:在原生AlertDialog添加EditText。

方法一:無需xml布局:??????

activity中顯示dialog的方法:

    private void showInputDialog(String msg, boolean onDutyStatus) {
        // 獲取EditText
        final EditText editText = new EditText(ClockInActivity.this);
        editText.setSingleLine();
        editText.setHint("請?zhí)顚懺?);
        editText.requestFocus();
        editText.setFocusable(true);
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(this)
                .setTitle("你確定要外勤打卡嗎\n請輸入原因:")
                .setView(editText).setPositiveButton("確定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String content = editText.getText().toString();
                                if (StrUtils.isEmpty(content)) {
                                    ToastUtil.show(ClockInActivity.this, "請?zhí)顚懺颍?);
                                    return;
                                }
                                // TODO 寫你的操作
                                ToastUtil.show(ClockInActivity.this, content);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
        inputDialog.create().show();
    }

效果如圖:


image.png

但是這樣會不美觀,即使要簡單,但也要自己能過得去眼,EditText有點過長了,就嘗試在代碼設(shè)置EditText === LayoutParams === setMargin,卻無效,希望有小伙伴可以指教。接著在setView的過程中,發(fā)現(xiàn)有setView(int layoutResId)的方法,考慮采用寫xml的方法,于是有了方法二,在xml里下手可以自如些,雖然最后用的還是setView(View view)的方法,但是卻可以解決不美觀的問題。

方法二:添加xml布局:??????

activity中顯示dialog的方法:

    private void showInputDialog(String msg) {
        LayoutInflater inflater = getLayoutInflater();
        final View layout = inflater.inflate(R.layout.dialog_edittext,
                (ViewGroup) findViewById(R.id.item_lin_ed));
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(this)
                .setTitle("你確定要外勤打卡嗎\n請輸入原因:")
                .setView(layout)
                .setPositiveButton("確定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 獲取EditText
                                EditText editText = layout.findViewById(R.id.item_ed);
                                String content = editText.getText().toString();
                                if (StrUtils.isEmpty(content)) {
                                    ToastUtil.show(ClockInActivity.this, "請輸入原因");
                                    return;
                                }
                                // TODO 寫你的操作
                                ToastUtil.show(ClockInActivity.this, content);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
        inputDialog.create().show();
    }

自定義view布局R.layout.dialog_edittext:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/item_lin_ed"
    >
    <EditText
        android:id="@+id/item_ed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入備注"
        android:layout_margin="20dp"
        />
</LinearLayout>

效果如圖:


image.png

以上。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容