分享一个简单的常用的对话框类,按照国际惯例,先上图
布局简单,先上布局。一个标题,一个内容,两个按钮
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <linearLayout androID:layout_wIDth="270dp" androID:layout_height="wrap_content" androID:layout_centerHorizontal="true" androID:layout_centerVertical="true" androID:background="@drawable/shape_diglog_bg" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/dialog_Title" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:layout_margintop="10dp" androID:text="这里是标题" androID:textcolor="#333333" androID:textSize="19sp" androID:visibility="visible" /> <TextVIEw androID:ID="@+ID/dialog_content" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:layout_marginBottom="30dp" androID:layout_marginleft="10dp" androID:layout_marginRight="10dp" androID:layout_margintop="30dp" androID:text="这里是内容" androID:textcolor="#333333" androID:textSize="17sp" androID:textStyle="bold" /> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="1dp" androID:layout_margintop="18sp" androID:background="#f1f1f1" /> <linearLayout androID:ID="@+ID/ll_button" androID:layout_wIDth="match_parent" androID:layout_height="50dp" androID:orIEntation="horizontal"> <button androID:ID="@+ID/dialog_cancel" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:background="@null" androID:text="取消" androID:textcolor="#006DFF" androID:textSize="17sp" /> <VIEw androID:layout_wIDth="1dp" androID:layout_height="match_parent" androID:background="#f1f1f1" /> <button androID:ID="@+ID/dialog_ensure" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:background="@null" androID:text="确定" androID:textcolor="#006DFF" androID:textSize="17sp" /> </linearLayout> </linearLayout></relativeLayout>
接着就是自定义类!
首先,新建类继承Dialog
public class CommonDialog extends Dialog
接着是构造函数,在构造函数中定义样式
public CommonDialog(@NonNull Context context) { super(context,R.style.dialog_Common); mContext = context; setContentVIEw(R.layout.dialog_common); ButterKnife.bind(this); }
其中,在style中定义对话框属性
<style name="dialog_Common" parent="androID:style/theme.Dialog"> <!--说明提示框是否有边框--> <item name="androID:windowFrame">@null</item> <!--说明提示框是否有标题--> <item name="androID:windowNoTitle">true</item> <!--设置提示框的背景颜色是什么--> <item name="androID:windowBackground">@androID:color/transparent</item> <!--是否浮现在activity之上--> <item name="androID:windowIsfloating">true</item> <!--是否有遮盖--> <item name="androID:windowContentOverlay">@null</item> <!--说明提示框是滞是透明的--> <item name="androID:windowIsTranslucent">true</item> <!--说明是否充许对话框的背景变暗。为true则充许变暗--> <item name="androID:backgroundDimEnabled">true</item> <!--设置背景透明度--> <item name="androID:backgroundDimAmount">0.4</item> </style>
紧接着,提供四个变量来存储设置的标题、内容以及两个按钮的文字
private String Title;private String content;private String cancel;private String ensure;
现在需要提供能够设置标题、内容以及两个按钮的文字的方法
/* * 设置标题 默认没有标题 * * @param Title */ public voID setTitle(String Title) { this.Title = Title; } /** * 设置内容 默认为空 * * @param content */ public voID setContent(String content) { this.content = content; } /** * 设置确定按钮内容 默认为确定 * * @param ensure */ public voID setEnsure(String ensure) { this.ensure = ensure; } /** * 设置取消按钮内容 默认为取消 * * @param cancel */ public voID setCancel(String cancel) { this.cancel = cancel; }
现在,处理按钮的点击事件
/** * 确定按钮事件监听 默认是dismiss对话框 * * @param onEnsureClickListener */ public voID setonEnsureClickListener(VIEw.OnClickListener onEnsureClickListener) { this.onEnsureClickListener = onEnsureClickListener; } /** * 取消按钮事件监听 默认是dismiss对话框 * * @param onCabcelClickListener */ public voID setonCancelClickListener(VIEw.OnClickListener onCabcelClickListener) { this.onCancelClickListener = onCabcelClickListener; }
默认的是点击对话框消失
/** * 默认点击事件,点击d框消失 */ private VIEw.OnClickListener onClickListenerdismiss = new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { dismiss(); } };
最后,重写父类的show方法,将展示之前设置的各种信息
/** * 重写show方法 */ @OverrIDe public voID show() { if (TextUtils.isEmpty(Title)) { //默认没有标题 dialogTitle.setVisibility(VIEw.GONE); } else { //默认不设置内容,则内容太为空 dialogTitle.setVisibility(VIEw.VISIBLE); setTextVIEwTxt(dialogTitle,Title); } if (TextUtils.isEmpty(cancel)) { //默认取消按钮文字为"取消" cancel = mContext.getString(R.string.cancel); } if (TextUtils.isEmpty(ensure)) { //默认确认按钮文字为"确认" ensure = mContext.getString(R.string.ensure); } //设置文字信息 setTextVIEwTxt(dialogContent,content); setTextVIEwTxt(dialogCancel,cancel); setTextVIEwTxt(dialogEnsure,ensure); //设置点击事件 setbuttonOnClickListener(dialogCancel,onCancelClickListener); setbuttonOnClickListener(dialogEnsure,onEnsureClickListener); super.show(); }
最最后,献上在Activity中如何使用该对话框的方法的代码
public class MainActivity extends AppCompatActivity { @BindVIEw(R.ID.btn_demo_haveTitle) button btnDemoHaveTitle; @BindVIEw(R.ID.btn_demo_noTitle) button btnDemoNoTitle; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); ButterKnife.bind(this); } @OnClick({R.ID.btn_demo_haveTitle,R.ID.btn_demo_noTitle}) public voID onClick(VIEw vIEw) { switch (vIEw.getID()) { //没有标题的对话框 case R.ID.btn_demo_haveTitle: final CommonDialog dialog1 = new CommonDialog(this); dialog1.setTitle("提示"); dialog1.setContent("是否确认退出?"); dialog1.setonEnsureClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Toast.makeText(MainActivity.this,"点击了确认",Toast.LENGTH_SHORT).show(); // Todo: 2017/9/17 这里写你的代码 dialog1.dismiss(); } }); dialog1.show(); break; //有标题的对话框 case R.ID.btn_demo_noTitle: final CommonDialog dialog2 = new CommonDialog(this); //不设置标题默认没有标题 dialog2.setContent("是否确认退出?"); dialog2.setonEnsureClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Toast.makeText(MainActivity.this,Toast.LENGTH_SHORT).show(); // Todo: 2017/9/17 这里写你的代码 dialog2.dismiss(); } }); dialog2.show(); break; } }}
总结
以上所述是小编给大家介绍的AndroID实现类似iOS风格的对话框样式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android实现类似iOS风格的对话框实例代码全部内容,希望文章能够帮你解决Android实现类似iOS风格的对话框实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)