Android开发之对话框案例详解(五种对话框)
作者:一猴当先 发布时间:2021-08-07 11:37:47
标签:android,开发,对话框
下面通过实例代码给大家分享5种android对话框,具体内容详情如下所示:
1 弹出普通对话框 --- 系统更新
2 自定义对话框-- 用户登录
3 时间选择对话框 -- 时间对话框
4 进度条对话框 -- 信息加载..
5 popuWindow对话框
1 弹出普通对话框 --- 系统更新
//弹出普通对话框
public void showNormalDialog(View v) {
AlertDialog.Builder builder = new Builder(this);
//设置Dialog的图标
builder.setIcon(R.drawable.ic_launcher);
//设置对话框的标题
builder.setTitle("更新");
//设置message
builder.setMessage("发现新版本是否更新?");
//确定按钮 取消按钮
builder.setPositiveButton("确定",new OnClickListener() {
/**
* 点击确定按钮 回调该方法
*/
@Override
public void onClick(DialogInterface dialog, int which) {
//到服务器去下载新的版本 duration单词意思:时长
Toast.makeText(MainActivity.this, "开始下载新版本", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
/**
* 点击取消按钮 回调该方法
*/
@Override
public void onClick(DialogInterface dialog, int which) {
//到服务器去下载新的版本 duration单词意思:时长
Toast.makeText(MainActivity.this, "不需要更新", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("下一次", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//到服务器去下载新的版本 duration单词意思:时长
Toast.makeText(MainActivity.this, "下一次吧", Toast.LENGTH_SHORT).show();
}
});
//通过建造这老构建一个对话框
Dialog dialog = builder.create();
//显示
dialog.show();
}
2 自定义对话框-- 用户登录
布局文件:
user_name_dialog.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:padding="10dip"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录信息"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"/>
<TextView
android:id="@+id/tv_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:" />
<EditText android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码"/>
<requestFocus />
<Button
android:id="@+id/btn_confirm"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="登录" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="取消" />
</LinearLayout>
java代码:
//自定义对话框
Dialog cus_dialog ;
public void showCustomDialog(View v){
AlertDialog.Builder builder = new Builder(this);
// 布局填充器
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.user_name_dialog, null);
// 设置自定义的对话框界面
builder.setView(view);
// 获取用户名密码
final EditText name = (EditText) view.findViewById(R.id.et_name);
final EditText pwd = (EditText) view.findViewById(R.id.et_pwd);
Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
btn_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
if(name.getText().toString().trim().equals("abc")){
showToastMsg("用户名正确");
// 对话框消失
cus_dialog.dismiss();
}
else{
showToastMsg("用户名错误");
}
}
});
Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 对话框消失
cus_dialog.dismiss();
}
});
cus_dialog = builder.create();
cus_dialog.show();
}
下载地址:http://www.jinhusns.com/Products/Download/?type=yhq
3 时间选择对话框 -- 时间对话框
// 时间选择对话框
public void showTimePickerDialog(View v){
Calendar sysDate = Calendar.getInstance();
//设置系统时间
sysDate.setTimeInMillis(System.currentTimeMillis());
int hour = sysDate.get(Calendar.HOUR_OF_DAY);
int minute = sysDate.get(Calendar.MINUTE);
TimePickerDialog time = new TimePickerDialog(this,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO 自动生成的方法存根
showToastMsg(" hourOfDay:" + hourOfDay + " minute:" + minute);
}
}, //callBack 选择时间后的回调方法
hour,//hourOfDay 当前系统时间
minute,//hourOfDay 当前系统时间
true);//是否24小时制
time.show();
}
4 进度条对话框 -- 信息加载..
/**
* 进度条对话框
* @param v
*/
public void showProgressDialog(View v){
final ProgressDialog progress = new ProgressDialog(this);
progress.setProgress(R.drawable.img2);
progress.setTitle("标题");
progress.setMessage("加载中...");
//样式1 进度条样式
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//样式2 有加载图标
//progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//最大
progress.setMax(100);
//当前
progress.setProgress(50);
progress.setButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自动生成的方法存根
showToastMsg("确定按钮");
}
});
progress.setButton2("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自动生成的方法存根
showToastMsg("取消按钮");
}
});
progress.show();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (i < 100) {
try {
Thread.sleep(200);
// 更新进度条的进度,可以在子线程中更新进度条进度
progress.incrementProgressBy(5);
// progress.incrementSecondaryProgressBy(10);//二级进度条更新方式
i += 5;
} catch (Exception e) {
// TODO: handle exception
}
}
// 在进度条走完时删除Dialog
progress.dismiss();
}
}).start();
}
5 popuWindow对话框
Button btn_popu;
//popuWindow对话框
public void showPopuWindow(View v){
btn_popu = (Button) v;
// 设置布局
View view = LayoutInflater.from(this).inflate(R.layout.pop_window, null);
PopupWindow window = new PopupWindow(this);
window.setContentView(view);
window.setWidth(360);
window.setHeight(200);
int[] location = new int[2];
// 获取按钮坐标
btn_popu.getLocationInWindow(location);
window.setFocusable(true);
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_null));
window.showAtLocation(btn_popu, Gravity.LEFT |Gravity.TOP , location[0]+ btn_popu.getWidth(), location[1] + 0 );
//showToastMsg("" + (location[0]+ btn_popu.getWidth())+" "+ (location[1] + btn_popu.getHeight() / 2));
ImageView img_start = (ImageView) view.findViewById(R.id.img_start);
img_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
showToastMsg("点击了启动");
}
});
}
总结
以上所述是小编给大家介绍的详解Android开发之对话框案例详解(五种对话框)网站的支持!
来源:http://www.cnblogs.com/yihoudangxian/archive/2017/09/12/7511361.html


猜你喜欢
- 如下所示:TextView tv = (TextView) findViewById(R.id.text); tv.getPaint().s
- 运行原理1、不同线程中所包含的栈帧是不允许存在相互引用的。2、如果当前方法调用了其他方法,方法返回之际,当前栈帧会传回此方法的执行结果给当前
- 本文实例讲述了C#简单实现显示中文格式星期几的方法。分享给大家供大家参考,具体如下:1.DateTime.Now.ToString(&quo
- 初步探索首先我们要了解equals方法是什么,hashcode方法是什么。equals方法equals 是java的obejct类的一个方法
- 有些人可能对线程池比较陌生,并且更不熟悉线程池的工作原理。所以他们在使用线程的时候,多数情况下都是new Thread来实现多线程。但是,往
- Dataway介绍Dataway 是基于 DataQL 服务聚合能力,为应用提供的一个接口配置工具。使得使用者无需开发任何代码就配置一个满足
- 因项目中的需要实现ScrollView顶部的悬停,也不是太难便自己实现功能,话不多说,先上效果图红色text一到顶上便会悬浮在上面,不会跟随
- 使用IDEA编辑Web项目已经逐渐超过了使用eclipse的人数,但是IDEA对于pom.xml的执行也就是Maven方式导包支持并不是很完
- 这篇文章主要介绍了Spring @Conditional注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 题目:使用struts2自定义 * ,完成用户登陆才能访问权限的实现在session中存放user变量表示用户登陆,若user为空则用户没有
- 前言中国象棋是起源于中国的一种棋,属于二人对抗 * 的一种,在中国有着悠久的历史。由于用具简单,趣味性强,成为流行极为广泛的棋艺活动。中国象
- 本文实例为大家分享了Unity3D实现物体旋转缩放移动的具体代码,供大家参考,具体内容如下由于项目运行在安卓上,运用到了插件,比较麻烦。你们
- 同一个service调用service本身如果同一个service调用service本身的方法,出现了事务不能控制。解决方案1.在sprin
- 本文实例讲述了Android7.0开发实现Launcher3去掉应用抽屉的方法。分享给大家供大家参考,具体如下:年初做过一个项目,有一个需求
- C#字符串提取数值(带小数点)string input = "树2草45210.2m2";if (GetInputUti
- 1、写在前面今天群里一个小伙伴问了这样一个问题,扩展方法与实例方法的执行顺序是什么样子的,谁先谁后(这个问题会在文章结尾回答)。所以写了这边
- 目录springboot autoconfig的一些实验SpringBoot autoconfig部分注解说明SpringBoot auto
- 前言首先我们初始化一个最简单的容器,用这个容器研究初始化的流程。下面就是一个再简单不过的IoC容器了,该容器包含了一个名为beanA的bea
- Spring Security简介Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的
- 本文实例讲述了Android编程简单实现九宫格。分享给大家供大家参考,具体如下:实现的步骤1. 一个整体的容器部分。就是上图中包括整个图片项