重写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>

效果: 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 Android 自定义对话框,你可以通过以下步骤来实现: 1. 创建一个新的类继承自 DialogFragment,这将作为你的自定义对话框实现类。 ```java public class CustomDialogFragment extends DialogFragment { // 在这里实现你的自定义对话框逻辑 } ``` 2. 在你的布局文件中定义自定义对话框的外观。可以使用 LinearLayout、RelativeLayout 或者其他布局容器来构建对话框的外观。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 在这里添加对话框的内容,比如文本、按钮等 --> </LinearLayout> ``` 3. 在自定义对话框实现类中,重写 `onCreateDialog` 方法,将布局文件与对话框关联起来。 ```java @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); View dialogView = inflater.inflate(R.layout.custom_dialog_layout, null); builder.setView(dialogView); // 在这里设置对话框的标题、按钮等内容 return builder.create(); } ``` 4. 在需要显示对话框的地方,创建并显示你的自定义对话框实例。 ```java CustomDialogFragment dialog = new CustomDialogFragment(); dialog.show(getSupportFragmentManager(), "custom_dialog"); ``` 这就是一个基本的 Android 自定义对话框实现过程。你可以进一步扩展自定义对话框的功能,根据你的需求添加文本、按钮、点击事件等等。希望这能帮到你!如果有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值