栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android自定义Dialog实现通用圆角对话框

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android自定义Dialog实现通用圆角对话框

前言:圆角对话框在项目中用的越来越多,之前一篇文章有介绍过使用系统的alertDialog+CardView(Android中使用CardView实现圆角对话框)实现了圆角对话框的样式,今天介绍自定义Dialog实现通用的圆角对话框。

效果图:

1.继承自alertDialog,重写onCreat



public class RoundCornerDialog extends alertDialog{

  private TextView tvTitle;
  private TextView tvDes;
  private TextView tvCancel;
  private TextView tv/confirm/i;
  //private Context context;

  
  public RoundCornerDialog(@NonNull Context context) {
    super(context);
    //this.context=context;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_layout_test);
    //设置背景透明,不然会出现白色直角问题
    Window window = getWindow();
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    setCanceledonTouchOutside(false);
    //初始化布局控件
    initView();
    //确定和取消按钮的事件监听
    initEvent();
    //设置参数必须在show之后,不然没有效果
    WindowManager.LayoutParams params = getWindow().getAttributes();
    getWindow().setAttributes(params);
  }

}

注:解决白色直角的问题

(1)文中没有使用style设置背景透明,直接在代码中用的window.setBackgroundDrawable设置的背景透明,不然会出现遗留的四个角有白色直角的问题。

(2)当然也可以在构造方法中这样设置:super(context,R.style.CustomDialog)。

2.初始化布局

(1)布局文件(CradView实现圆角布局)



  

    

    

    

    

    

      

      

      
    
  

(2)初始化布局文件及设置参数


private void initView() {
  //对话框标题
  tvTitle=findViewById(R.id.tv_title);
  //对话框描述信息
  tvDes=findViewById(R.id.tv_des);
  //确定按钮和取消
  tvConfirm=findViewById(R.id.tv_/confirm/i);
  tvCancel=findViewById(R.id.tv_cancel);
  
}

(3)设置事件监听

让自定义的dialog实现OnClickListener接口,然后设置确定及取消按钮的事件监听


private void initEvent() {
  tv/confirm/i.setonClickListener(this);//确定
  tvCancel.setonClickListener(this);//取消@Override
public void onClick(View view) {
  switch (view.getId()){
    case R.id.tv_/confirm/i:
      dismiss();
      break;
    case R.id.tv_cancel:
      dismiss();
      break;
  }
}

写到这里,圆角对话框就实现了,但如果另一个页面要求不同背景色,按钮的文本也不是“确定”和“取消”呢,我们是不是又的重写定义dialog和设置布局文件呢,显然这样很麻烦,貌似与我们的标题写的通用的圆角对话框也不相符啊,这似乎不太好吧。接下来,我们进行一番改造,打造通用的圆角对话框。

3.打造通用圆角对话框

(1)initView中设置初始参数

private String title="温馨提示",message,confirmText="确定",cancelText="取消";
//默认的标题栏背景色
private int titleColorBg=Color.parseColor("#FF8200");
//默认的确定和取消按钮背景色
private int confirmColorBg=Color.parseColor("#F8F8F8");
private int cancelColorBg=Color.parseColor("#F8F8F8");

private void initView() {
  //对话框标题
  tvTitle=findViewById(R.id.tv_title);
  //对话框描述信息
  tvDes=findViewById(R.id.tv_des);
  //确定按钮和取消
  tvConfirm=findViewById(R.id.tv_/confirm/i);
  tvCancel=findViewById(R.id.tv_cancel);
 
  //设置标题、描述及确定按钮的文本内容
  tvTitle.setText(title);
  tvDes.setText(message);
  tv/confirm/i.setText(/confirm/iText);
  //设置标题栏及确定、取消按钮背景色
  tvTitle.setBackgroundColor(titleColorBg);
  tv/confirm/i.setBackgroundColor(/confirm/iColorBg);
  tvCancel.setBackgroundColor(cancelColorBg);
}

(2)定义设置属性方法


public void setTitle(String title){
  this.title=title;
}


public void setMessage(String message){
  this.message=message;
}


public void setConfirmText(String /confirm/iText){
  this.confirmText=/confirm/iText;
}


public void setCancelText(String cancelText){
  this.cancelText=cancelText;
}


public void setTitleBg(int titleColorBg){
  this.titleColorBg=titleColorBg;
}


public void setConfirmBg(int /confirm/iColorBg){
  this.confirmColorBg=/confirm/iColorBg;
}


public void setCancelBg(int cancelColorBg){
  this.cancelColorBg=cancelColorBg;
}

(3)定义接口,实现确定按钮的点击回调

private ConfirmListener /confirm/iListener;


public void setConfirmListener(ConfirmListener /confirm/iListener){
  this.confirmListener=/confirm/iListener;
}


public interface /confirm/iListener{
  void on/confirm/iClick();
}

点击“确定”回调方法

case R.id.tv_/confirm/i:
  
  //点击确定按钮回调
  /confirm/iListener.on/confirm/iClick();
  dismiss();
  break;

一般点击“取消”按钮不做任何操作,只是关闭当前弹出的对话框,所以这里不做点击后回调,当然,点击“确定”后执行相关操作后也要关闭当前dialog。

4.使用

RoundCornerDialog roundCornerDialog=new RoundCornerDialog(mContext);
//设置标题,描述,文本等参数
roundCornerDialog.setTitle("温馨提示");
roundCornerDialog.setMessage("退出当前登录后将要重新登录!");
roundCornerDialog.setConfirmText("确认退出");
//确定按钮的点击回调方法
roundCornerDialog.setConfirmListener(new roundCornerDialog.ConfirmListener() {
  @Override
  public void on/confirm/iClick() {
    Intent intent = new Intent(mContext, LoginActivity.class);
    startActivity(intent);
    UIUtil.toast("退出成功,请重新登录");
    getActivity().finish();
  }
});
//显示对话框
roundCornerDialog.show();

总结:本文通过自定义Dialog+CardView的方式实现了通用的圆角对话框效果,使用也相对简单,测试中发现在Android5.0以下设置标题栏背景色时,标题栏不会跟随CardView的圆角。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/155290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号