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.效果图
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)