重写AlertDialog实现自定义的对话框

 实现自定义的对话框可以直接通过set方法传入XML布局,但是显得代码过于臃肿,所以下面的方法是继承了AlertDialog类,自己定义一个类,这种方法更方便。

继承了AlertDialog重写其中的onCreate,onClick方法,改写为自己的逻辑。

public class CustomDialog extends AlertDialog implements View.OnClickListener {
    Context mContext;
    private EditText editText;
    private Button mBtnCancel, mBtnConfirm;

    public CustomDialog(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //指定为自己的布局
        setContentView(R.layout.add_group);
        editText = findViewById(R.id.edit_new_group);
        mBtnCancel = findViewById(R.id.btn_cancel);
        mBtnConfirm = findViewById(R.id.btn_confirm);
        mBtnCancel.setOnClickListener(this);
        mBtnConfirm.setOnClickListener(this);
        Window window = this.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// 有白色背景,加这句代码
        //保证EditText能弹出键盘
        this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        this.setCancelable(false);
    }

    @Override
    public void onClick(View view) {
        //设置弹出Dialog的相应事件
        switch (view.getId()) {
            case R.id.btn_cancel:
                this.dismiss();
                break;
            case R.id.btn_confirm:
                if (TextUtils.isEmpty(editText.getText())) {
                    Toast.makeText(mContext, "不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, editText.getText().toString(), Toast.LENGTH_SHORT).show();
                    NoteGroup group = new NoteGroup();
                    group.setCreateTime(CommonUtil.date2string(new Date()));
                    group.setUpdateTime(group.getCreateTime());
                    group.setName(editText.getText().toString());
                    NoteGroupLitepal.createNewGroup(group);
                    this.dismiss();
                }
                break;
            default:
                break;
        }
    }

xml布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/activity_margin_20"
            android:layout_marginTop="@dimen/activity_margin_20"
            android:gravity="center"
            android:text="新建文件夹"
            android:textColor="@android:color/black" />

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/activity_margin_20"
            android:layout_marginEnd="@dimen/activity_margin_20"
            android:layout_marginStart="@dimen/activity_margin_20"
            app:cardCornerRadius="5dp"
            app:cardElevation="0dp">

            <EditText
                android:id="@+id/edit_new_group"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#f9f9f9" />
        </android.support.v7.widget.CardView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="@drawable/shape_button"
                android:text="取消" />

            <Button
                android:id="@+id/btn_confirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="@drawable/shape_button"
                android:text="确定" />
        </LinearLayout>
    </LinearLayout>

</android.support.v7.widget.CardView>

效果: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值