Android 自定义一套 Dialog通用提示框 (代码库)
作者:小小情意 发布时间:2022-10-08 06:20:31
做Android开发五年了,期间做做停停(去做后台开发,服务器管理),当回来做Android的时候,发现很生疏,好些控件以前写得很顺手,现在好像忘记些什么了,总要打开这个项目,打开那个项目,有时未必还找得到。
总结起来,还是源于没有好好做一个属于自己的代码库,把平时开发项目中一些自定义的控件,或一些耦合性很低的模块封装起来,或者平时比较少写博客。如果你是一个刚学会开发的程序猿,或者是有过好几年开发经验的大鸟,也该开始整理整理自己的代码,这也不枉此生敲代码的岁月,同时在面试中,也会给你带来不少印象分喔。所以,我也开始准备自己的代码库,放在github 或者微博上,希望可以跟各位大神多交流。下面我先放一到两个自定义控件。
自定义一套 Dialog通用提示框:
上诉提示框都是一种类型,当然有可能你不大满意,或者与你们设计师的要求的风格不一致,没关系,你只要进去修改一下dialog 的布局就可以了。当然,我希望在自定义这些控件的时候,能用xml 来渲染的,尽量不要用图片去做背景之类的。每个app 的提示框风格其实大体一致的,不会每个页面的提示框都不一样,如果真的变化太大,我们就重新自定义一个dialog即可。其它的只需设置一下信息即可:
new CommomDialog(mContext, R.style.dialog, "您确定删除此信息?", new CommomDialog.OnCloseListener() {
@Override
public void onClick(boolean confirm) {
if(confirm){
Toast.makeText(this,"点击确定", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
})
.setTitle("提示").show();
我们先看 CommomDialog 类, 这个类定义的时候,里面的方法尽量做成链式的,方便后期调用
public class CommomDialog extends Dialog implements View.OnClickListener{
private TextView contentTxt;
private TextView titleTxt;
private TextView submitTxt;
private TextView cancelTxt;
private Context mContext;
private String content;
private OnCloseListener listener;
private String positiveName;
private String negativeName;
private String title;
public CommomDialog(Context context) {
super(context);
this.mContext = context;
}
public CommomDialog(Context context, int themeResId, String content) {
super(context, themeResId);
this.mContext = context;
this.content = content;
}
public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener) {
super(context, themeResId);
this.mContext = context;
this.content = content;
this.listener = listener;
}
protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
this.mContext = context;
}
public CommomDialog setTitle(String title){
this.title = title;
return this;
}
public CommomDialog setPositiveButton(String name){
this.positiveName = name;
return this;
}
public CommomDialog setNegativeButton(String name){
this.negativeName = name;
return this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_commom);
setCanceledOnTouchOutside(false);
initView();
}
private void initView(){
contentTxt = (TextView)findViewById(R.id.content);
titleTxt = (TextView)findViewById(R.id.title);
submitTxt = (TextView)findViewById(R.id.submit);
submitTxt.setOnClickListener(this);
cancelTxt = (TextView)findViewById(R.id.cancel);
cancelTxt.setOnClickListener(this);
contentTxt.setText(content);
if(!TextUtils.isEmpty(positiveName)){
submitTxt.setText(positiveName);
}
if(!TextUtils.isEmpty(negativeName)){
cancelTxt.setText(negativeName);
}
if(!TextUtils.isEmpty(title)){
titleTxt.setText(title);
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.cancel:
if(listener != null){
listener.onClick(this, false);
}
this.dismiss();
break;
case R.id.submit:
if(listener != null){
listener.onClick(this, true);
}
break;
}
}
public interface OnCloseListener{
void onClick(Dialog dialog, boolean confirm);
}
}
自定义了监听事件,设置了消息后,返回该句柄, return this;
再看看 R.layout.dialog_commom 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:background="@drawable/bg_round_white"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="12dp"
android:layout_marginTop="12dp"
android:text="提示"
android:textSize="16sp"
android:textColor="@color/black"/>
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:lineSpacingExtra="3dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="30dp"
android:text="签到成功,获得200积分"
android:textSize="12sp"
android:textColor="@color/font_common_1"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/commom_background"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_dialog_left_white"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/cancel"
android:textSize="12sp"
android:textColor="@color/font_common_2"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/commom_background"/>
<TextView
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_dialog_right_white"
android:gravity="center"
android:layout_weight="1.0"
android:text="@string/submit"
android:textSize="12sp"
android:textColor="@color/font_blue"/>
</LinearLayout>
</LinearLayout>
整个背景我使用了圆角,这样不显得特别生硬 android:background="@drawable/bg_round_white"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/white" />
<corners android:radius="8dp" />
</shape>
当然底部两个按钮也是要做相应的圆角处理:
左下按钮:android:background="@drawable/bg_dialog_left_white"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/white" />
<corners android:bottomLeftRadius="8dp" />
</shape>
右下按钮:android:background="@drawable/bg_dialog_right_white"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/white" />
<corners android:bottomRightRadius="8dp" />
</shape>
展示的 style 也要设置一下:
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<!--边框-->
<item name="android:windowIsFloating">true</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">false</item>
<!--半透明-->
<item name="android:windowNoTitle">true</item>
<!--无标题-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--背景透明-->
<item name="android:backgroundDimEnabled">true</item>
<!--模糊-->
</style>
这样基本大功告成,通过设置消息头,信息体,按钮名称,还有点击事件,就可以随意控制你的提示框了。
源码链接:https://github.com/xiaoxiaoqingyi/mine-android-repository
后面我会把自己的代码库都放上来,与大家一起学习。
以上所述是小编给大家介绍的Android 自定义一套 Dialog通用提示框 (代码库)网站的支持!
来源:http://www.cnblogs.com/xiaoxiaoqingyi/p/6738108.html


猜你喜欢
- CountDownLatch在日常开发中经常会遇到需要在主线程中开启多个线程去并行执行任务,并且主线程需要等待所有子线程执行完毕后再进行汇总
- 一、简介Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程。该框架使用
- 简介目的:Optional的出现主要是为了解决null指针问题,也叫NPE(NullPointerException)外形:Optional
- 详解java.lang.reflect.Modifier.isInterface()方法java.lang.reflect.Modifier
- 一、项目简述功能包括: 该系统不错分为学生,教师,管理员,教导主任四种角 色,包括学生管理,教师管理,学生选题,教师选题,主 任审核,管理员
- 常用事件的分类Java AWT里面的事件可以简单的分为窗体事件(WindowEvent),鼠标事件(MouseEvent),键盘事件(Key
- 解决方案1:禁用缓存,前一次使用的方法,在电脑上各浏览器都没问题,但在ipad、安卓手机上仍有问题解决方案2:禁用浏览器后退键 javasc
- Android 自定义 Toast 显示时间实现代码:package com.wm.realname.util;import android
- JavaMail API中定义了一个java.mail.Transport类,它专门用于执行邮件发送任务,这个类的实例对象封装了某种邮件发送
- 面试中可能会被问到为什么我们调用start()方法时会执行run()方法,为什么我们不能直接调用run()方法?Java 创建线程的方法实际
- 首先定义两个示例类ClassA,ClassB,用于后续的示例演示package cn.lzrabbit;public class Class
- 本文实例为大家分享了unity实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下首先创建一个头部,编写脚本利用WASD控制头部的移动。Vec
- c#里面封装了几乎所有我们可以想到的和我们没有想到的类,流是读取文件的一般手段,那么你真的会用它读取文件中的数据了么?真的能读完全么?通常我
- 本文实例讲述了Android开发之DatePickerDialog、TimePickerDialog时间日期对话框用法。分享给大家供大家参考
- 前言:事情是这样的:运营人员反馈,通过Excel导入数据时,有一部分成功了,有一部分未导入。初步猜测,是事务未生效导致的。查看代码,发现导入
- 如果只想查看注解,请跳到文章末尾部分简介在前后端进行数据交互中,在前端把数据传送到后端前,一般会先进行校验一次,校验成功之后,才把数据发送到
- 本文实例讲述了Android编程中context及全局变量的用法。分享给大家供大家参考,具体如下:今天在研究context的时候,对appl
- 日记基础操作编程期调试代码运营期记录信息记录日常运营重要信息(峰值流量,平均响应时长...)记录应用报错信息(错误堆栈)记录运维过程数据(扩
- 一、前言本篇文章聚焦在“如何使用FragmentStatePagerAdapter来保存Fragment的数据、在内存中
- 1. 三种常用的字符串判空串方法:Length法:bool isEmpty = (str.Length == 0);Empty法:bool