Android中仿IOS提示框的实现方法
作者:dreamGong 时间:2023-01-06 15:47:07
前言
在Android开发中,我们有时需要实现类似IOS的对话框。今天我就来总结下,如何通过自定义的开发来实现类似的功能。
自定义Dialog
我们知道Android中最常用的对话框就是Dialog及其派生类。这次我们通过组合的方式来实现一个类似IOS对话框的效果。我们先来看一下布局效果,这个相信大家都能弄出来,在这里我就贴一下最后的效果图(注意:对话框的边缘是圆角的)。
效果图如下:
我们看到,这个和IOS的对话框已经非常相似了,后面我们需要做的就是将其作为一个组件封装起来,实现AlertDialog那样的调用方式提供给调用者使用。下面我们来看一下整个的封装过程。
1、组合使用Dialog进行对象的构建
/**
* 创建BaseDialog实例
* @return
*/
public BaseDialog builder(){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDialog = new Dialog(mContext, R.style.Dialog);
//设置dialog弹出后会点击屏幕,dialog不消失;点击物理返回键dialog消失
mDialog.setCanceledOnTouchOutside(false);
View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
mDialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
//设置Dialog中展示的msg
mMessage=(TextView) layout.findViewById(R.id.txtMsg);
//设置确认按钮的处理事件
mPositiveBtnText=(TextView) layout.findViewById(R.id.txtSubmit);
mPositiveBtnText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mPositiveBtnClickListener != null) {
mPositiveBtnClickListener.onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
}
}
});
mNegativeBtnText=(TextView) layout.findViewById(R.id.txtCancle);
mNegativeBtnText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mNegativeBtnClickListener!=null) {
mNegativeBtnClickListener.onClick(mDialog, DialogInterface.BUTTON_NEGATIVE);
}
}
});
mDialog.setContentView(layout);
return this;
}
在这段代码中,我们看到第7行代码生成一个Dialog对象,然后通过解析样式文件(XML)通过setContentView方法添加到生成的Dialog对象中。下面的代码我们看到通过解析XML样式文件,来设置Dialog显示的文本、取消、确认按钮触发的事件等操作。重点在最后一行代码return this而不是mDialog。因为通过返回this对象我们可以使用“链式调用”来不断的进行调用设置等操作,类似Android中的AlertDialog使用方式。
2、自定义Dialog的一些设置
/**
* 设置Msg
* 支持Resource设置
* @param message
*/
public BaseDialog setMessage(int message){
mMessage.setText(mContext.getText(message).toString());
return this;
}
/**
* 设置Msg
* @param message
*/
public BaseDialog setMessage(String message){
mMessage.setText(message);
return this;
}
public BaseDialog setPositiveButton(int positiveBtnText,DialogInterface.OnClickListener listener) {
mPositiveBtnText.setText(mContext.getText(positiveBtnText));
mPositiveBtnClickListener = listener;
return this;
}
public BaseDialog setPositiveButton(String positiveBtnText,DialogInterface.OnClickListener listener){
mPositiveBtnText.setText(positiveBtnText);
mPositiveBtnClickListener=listener;
return this;
}
public BaseDialog setNegativeButton(int negativeBtnText,DialogInterface.OnClickListener listener){
mNegativeBtnText.setText(mContext.getText(negativeBtnText));
mNegativeBtnClickListener=listener;
return this;
}
public BaseDialog setNegativeButton(String negativeBtnText,DialogInterface.OnClickListener listener){
mNegativeBtnText.setText(negativeBtnText);
mNegativeBtnClickListener=listener;
return this;
}
public void show(){
mDialog.show();
}
我们看到我们提供了几个比较常用的函数。特别是setPositiveButton等方法,我们直接将事件处理对象传递进来了。下面我们来看下自定义控件的字段定义。代码如下:
public BaseDialog(Context context){
mContext=context;
}
private Dialog mDialog;
protected Context mContext;
protected TextView mMessage;
protected TextView mPositiveBtnText;
protected TextView mNegativeBtnText;
protected DialogInterface.OnClickListener mPositiveBtnClickListener;
protected DialogInterface.OnClickListener mNegativeBtnClickListener;
我们看到整个自定义Dialog的核心就是mDialog对象,通过组合的方式我们在每一个BaseDialog对象内部都会有一个Dialog对象,BaseDailog对象只是提供了一些外围的显示功能。核心的例如隐藏、显示、弹出效果等方式依赖于嵌入的mDialog对象的。
下面我们来看一下自定义Dialog的调用方式。代码如下:
/**
* 弹出电话确认按钮
*/
new BaseDialog(getActivity())
.builder()
.setMessage("15895991339")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:15895991339"));
startActivity(intent);
dialog.dismiss();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
我们看到,调用方式和AlertDialog的使用方式很相似。
来源:https://www.cnblogs.com/dreamGong/p/5882348.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java反射之Method的invoke方法实现教程详解
![](https://img.aspxhome.com/file/2023/2/66632_0s.png)
Android实现歌词渐变色和进度的效果
![](https://img.aspxhome.com/file/2023/8/91708_0s.png)
java开发实现订阅到货通知帮我们买到想买的东西
![](https://img.aspxhome.com/file/2023/6/77366_0s.png)
详解SpringMVC @RequestBody接收Json对象字符串
Java 字符终端上获取输入三种的方式分享
详解Java程序读取properties配置文件的方法
一文教你如何使用Databinding写一个关注功能
![](https://img.aspxhome.com/file/2023/0/88350_0s.png)
三行Android代码实现白天夜间模式流畅切换
![](https://img.aspxhome.com/file/2023/7/139117_0s.gif)
关于spring的自定义缓存注解分析
学会使用C#异常
![](https://img.aspxhome.com/file/2023/2/78192_0s.png)
C#微信公众号开发之消息处理
![](https://img.aspxhome.com/file/2023/9/100169_0s.png)
Android BottomNavigationView结合ViewPager实现底部导航栏步骤详解
Java编程使用卡片布局管理器示例【基于swing组件】
![](https://img.aspxhome.com/file/2023/3/76193_0s.jpg)
Android Retrofit原理深入探索
Android中把bitmap存成BMP格式图片的方法
详解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
![](https://img.aspxhome.com/file/2023/4/77394_0s.png)
Android PhoneWindowManager监听屏幕右侧向左滑动实现返回功能
java递归菜单树转换成pojo对象
Mybatis条件if test如何使用枚举值
![](https://img.aspxhome.com/file/2023/1/83051_0s.png)
SSH原理及两种登录方法图文详解
![](https://img.aspxhome.com/file/2023/8/59038_0s.png)