024 Android 自定义样式对话框(AlertDialog)

1.AlertDialog介绍

AlertDialog并不需要到布局文件中创建,而是在代码中通过构造器(AlertDialog.Builder)来构造标题、图标和按钮等内容的。

常规使用步骤(具体参见Android 开发博客中的024篇):

(1)创建构造器AlertDialog.Builder的对象;
(2)通过构造器的对象调用setTitle、setMessage等方法构造对话框的标题、信息和图标等内容;
(3)根据需要,设置正面按钮、负面按钮和中立按钮;
(4)调用create方法创建AlertDialog的对象;
(5)AlertDialog的对象调用show方法,让对话框在界面上显示。

只显示简单的标题和信息是满足不了我们的要求,比如我们要实现一个登录对话框的话,那就需要在对话框上放置EditText输入框了。AlertDialog早就为我们准备好了setView方法,只要往里面放进我们需要显示的View对象就可以了。

2.自定义对话框

(1)xml页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--android:background="#fcc" 会覆盖掉style中原本设置的background属性值-->
    <TextView
        style="@style/TitleStyle"
        android:background="#fcc"
        android:text="添加黑名单号码" />

    <EditText
        android:id="@+id/et_black_phone_call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:hint="请输入拦截号码" />

    <RadioGroup
        android:id="@+id/rg_black_call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <!--android:checked="true" 默认选中条目-->
        <RadioButton
            android:id="@+id/rb_sms"
            android:text="短信"
            android:textColor="#000"
            android:checked="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/rb_phone"
            android:text="电话"
            android:textColor="#000"
            android:layout_marginLeft="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/rb_all"
            android:text="所有"
            android:textColor="#000"
            android:layout_marginLeft="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/bt_black_confirm"
            android:layout_width="0dp"
            android:text="确认"
            android:textColor="#000"
            android:background="@drawable/selector_black_call_btn_bg"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/bt_black_cancel"
            android:layout_width="0dp"
            android:text="取消"
            android:textColor="#000"
            android:background="@drawable/selector_black_call_btn_bg"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

(2)java后台代码

private void showDialog() {
        //采用自定义的对话框样式,利用dialog.setView(view);
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        final AlertDialog dialog=builder.create();
        final View view=View.inflate(this,R.layout.dialog_set_black_call_list,null);
        dialog.setView(view);
        dialog.show();//显示对话框

        //找到自定义对话框布局文件中的控件
        final EditText et_black_phone_call=view.findViewById(R.id.et_black_phone_call);
        RadioGroup radioGroup=view.findViewById(R.id.rg_black_call);
        Button bt_black_confirm=view.findViewById(R.id.bt_black_confirm);
        Button bt_black_cancel=view.findViewById(R.id.bt_black_cancel);

        //监听radioGroup选中条目的切换过程
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_sms:
                        function_type="拦截短信";
                        break;
                    case R.id.rb_phone:
                        function_type="拦截电话";
                        break;
                    case R.id.rb_all:
                        function_type="拦截所有";
                        break;
                }
            }
        });

        bt_black_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1.获取输入框的电话号码
                String phoneNumber=et_black_phone_call.getText().toString();
                if(!TextUtils.isEmpty(phoneNumber)){
                    //2.向数据库中插入当前用户输入的拦截号码
                    BlackListCallDBUtil.insertOneRecord(phoneNumber,function_type);
                    initData();
                }else {
                    Toast.makeText(getApplicationContext(),"请输入电话号码",Toast.LENGTH_SHORT).show();
                }
            }
        });

        bt_black_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss(); //关闭对话框
            }
        });
    }

3.效果图

转载于:https://www.cnblogs.com/luckyplj/p/10847912.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值