摘要:默认的太单调,我们可以通过继承原生的来实现自定义的。本文的自定义和原生的的创建方式类似,通过一个静态类来设置的图标标题内容和按钮。如果想要在中使用输入框或者其他控件,方法也是类似的,只要写好布局再加载就可以了。
Android默认的AlertDialog太单调,我们可以通过继承原生的Dialog来实现自定义的Dialog。
本文的自定义Dialog和原生的AlertDialog的创建方式类似,通过一个静态Builder类来设置Dialog的图标、标题、内容和按钮。
如果想要在Dialog中使用输入框或者其他控件,方法也是类似的,只要写好布局再加载就可以了。
Github:https://github.com/imcloudfloating/DesignApp
效果:
布局文件代码:
(注意这里的根布局的宽高如果用match_parent或者设置为具体的数值都和wrap_conten效果一样,可以通过设置子控件的大小来撑开)
1 29 10 21 22 23 42 4329 30 31 40 41 53 54 55 75 7661 62 73 74
InfoDialog类:
1 package com.cloud.design.dialog; 2 3 import android.app.Dialog; 4 import android.content.Context; 5 import android.graphics.Bitmap; 6 import android.support.annotation.NonNull; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.Button; 11 import android.widget.ImageView; 12 import android.widget.TextView; 13 14 import com.cloud.design.R; 15 16 public class InfoDialog extends Dialog { 17 18 private InfoDialog(Context context, int themeResId) { 19 super(context, themeResId); 20 } 21 22 public static class Builder { 23 24 private View mLayout; 25 26 private ImageView mIcon; 27 private TextView mTitle; 28 private TextView mMessage; 29 private Button mButton; 30 31 private View.OnClickListener mButtonClickListener; 32 33 private InfoDialog mDialog; 34 35 public Builder(Context context) { 36 mDialog = new InfoDialog(context, R.style.Theme_AppCompat_Dialog); 37 LayoutInflater inflater = 38 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 39 //加载布局文件 40 mLayout = inflater.inflate(R.layout.dialog, null, false); 41 //添加布局文件到 Dialog 42 mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 43 ViewGroup.LayoutParams.WRAP_CONTENT)); 44 45 mIcon = mLayout.findViewById(R.id.dialog_icon); 46 mTitle = mLayout.findViewById(R.id.dialog_title); 47 mMessage = mLayout.findViewById(R.id.dialog_message); 48 mButton = mLayout.findViewById(R.id.dialog_button); 49 } 50 51 /** 52 * 通过 ID 设置 Dialog 图标 53 */ 54 public Builder setIcon(int resId) { 55 mIcon.setImageResource(resId); 56 return this; 57 } 58 59 /** 60 * 用 Bitmap 作为 Dialog 图标 61 */ 62 public Builder setIcon(Bitmap bitmap) { 63 mIcon.setImageBitmap(bitmap); 64 return this; 65 } 66 67 /** 68 * 设置 Dialog 标题 69 */ 70 public Builder setTitle(@NonNull String title) { 71 mTitle.setText(title); 72 mTitle.setVisibility(View.VISIBLE); 73 return this; 74 } 75 76 /** 77 * 设置 Message 78 */ 79 public Builder setMessage(@NonNull String message) { 80 mMessage.setText(message); 81 return this; 82 } 83 84 /** 85 * 设置按钮文字和监听 86 */ 87 public Builder setButton(@NonNull String text, View.OnClickListener listener) { 88 mButton.setText(text); 89 mButtonClickListener = listener; 90 return this; 91 } 92 93 public InfoDialog create() { 94 mButton.setOnClickListener(view -> { 95 mDialog.dismiss(); 96 mButtonClickListener.onClick(view); 97 }); 98 mDialog.setContentView(mLayout); 99 mDialog.setCancelable(true); //用户可以点击后退键关闭 Dialog 100 mDialog.setCanceledOnTouchOutside(false); //用户不可以点击外部来关闭 Dialog 101 return mDialog; 102 } 103 } 104 }
弹出:
1 public class MainActivity extends AppCompatActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 findViewById(R.id.button_show_dialog).setOnClickListener(v -> { 9 InfoDialog infoDialog = new InfoDialog.Builder(this) 10 .setTitle("Done") 11 .setMessage("Something done") 12 .setButton("OK", view -> 13 Toast.makeText(this, "OK Clicked.", Toast.LENGTH_SHORT).show() 14 ).create(); 15 infoDialog.show(); 16 }); 17 } 18 }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/2677.html
摘要:对话框是提示用户作出决定或输入额外信息的小窗口。对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件。简介继承关系如下基本样式解析标题这是可选项,只应在内容区域被详细消息列表或自定义布局占据时使用。 showImg(https://segmentfault.com/img/remote/1460000019975019?w=157&h=54); 极力推荐文章:欢迎收藏An...
摘要:但是作为的基础控件之一,掌握它是十分有必要。创建构造器的对象通过构造器的对象调用等方法构造对话框的标题信息和图标等内容根据需要,设置正面按钮负面按钮和中立按钮调用方法创建的对象的对象调用方法,让对话框在界面上显示。参考文献谷歌文档 坦白说,AlertDialog我在工作中用得并不多,因为AlertDialog的样式比较固定和呆板,为了和App的整体设计匹配,一般都是使用自定义的Dial...
摘要:注为了便于区分,本文将选择年月日的控件称为日期选择控件,将选择时分的控件称为时间选择控件。初始化控件和创建相关变量初始化控件初始化控件,并为两个设置监听事件创建相关变量使用类获取当前的日期时间。 这个月根据需求在项目中做了一个时间选择器,虽然没有用到Android原生的时间选择控件,但我羞愧地发现自己竟然从来没有用过这方面控件!趁现在有时间,赶紧查缺补漏,写一篇博客吧。 (注:为了便于...
摘要:注为了便于区分,本文将选择年月日的控件称为日期选择控件,将选择时分的控件称为时间选择控件。初始化控件和创建相关变量初始化控件初始化控件,并为两个设置监听事件创建相关变量使用类获取当前的日期时间。 这个月根据需求在项目中做了一个时间选择器,虽然没有用到Android原生的时间选择控件,但我羞愧地发现自己竟然从来没有用过这方面控件!趁现在有时间,赶紧查缺补漏,写一篇博客吧。 (注:为了便于...
摘要:的基本方法创建设置标题图标设置标题这是一个对话框设置信息是否要跳转确定按钮取消按钮忽略显示对话框系统样式下拉列表选择一个城市下拉列表的数据源北京上海广州深圳杭州你选择的城市为单选框列表请选择性别男女第二个参数指定默认哪个单选框被勾中性别为确Dialog的基本方法 //创建Dialog AlertDialog.Builder builder = new AlertDialog.Builder...
阅读 2354·2021-11-23 09:51
阅读 2352·2021-09-30 09:48
阅读 845·2021-09-10 10:51
阅读 2059·2021-08-12 13:22
阅读 3412·2021-08-11 10:24
阅读 1907·2019-08-30 15:55
阅读 495·2019-08-30 14:05
阅读 3091·2019-08-30 13:03