Android开发 ---构建对话框Builder对象,消息提示框、列表对话框、单选提示框、多选提示框、日期/时间对话框、进度条对话框、自定义对话框、投影
效果图:
















1、activity_main.xml
描述:
a、定义了一个消息提示框按钮
点击按钮弹出消息
b、定义了一个选择城市的输入框
点击按钮选择城市
c、定义了一个单选提示框按钮
点击按钮选择某项内容
d、定义了一个多选提示框按钮
点击按钮选择多项内容
e、定义了一个选择入职日期的输入框
点击按钮选择入职日期
f、定义了一个选择上班时间的输入框
点击按钮选择时间
g、定义了一个进度条对话框按钮
点击按钮进行文件下载
h、定义了一个用户登录的按钮
点击按钮进行用户登录
点击这些按钮,弹出对话框完成相应的操作
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="消息提示框"
android:onClick="test_1"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/city"
android:hint="请选择城市"
android:gravity="center"
android:onClick="test_2"
<!--输入框提示信息的颜色为绿色-->
android:textColorHint="@android:color/holo_green_light"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选提示框"
android:onClick="test_3"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选提示框"
android:onClick="test_4"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/intime"
android:hint="入职日期"
android:gravity="center"
android:onClick="test_5"
android:textColorHint="@android:color/holo_green_light"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/workingTime"
android:hint="上班时间"
android:gravity="center"
android:onClick="test_6"
android:textColorHint="@android:color/holo_green_light"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进度条对话框"
android:onClick="test_7"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户登录"
android:onClick="test_8"
/>
</LinearLayout>
</ScrollView>
2、activity_main.xml
package com.nf.android_dialog; import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText ucity,intime,workTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ucity = (EditText)findViewById(R.id.city);
intime = (EditText)findViewById(R.id.intime);
workTime = (EditText)findViewById(R.id.workingTime);
}
//消息提示框
public void test_1(View view){
//在当前Activity中构建一个对话框Builder对象
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置builder将要构建的Dialog属性
//设置对话框的标题
builder.setTitle("消息");
//设置对话框的主要内容
builder.setMessage("今天12点,大家一起吃饭");
//设置对话框上的图标
builder.setIcon(R.mipmap.ic_launcher);
//对话框中绑定确定按钮
//当点击确定按钮是,new一个DialogInterface的单击监听事件的方法
builder.setPositiveButton("确定",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface,int i){
//当点击按钮时,弹出对话内容“你点击了确定按钮”
Toast.makeText(MainActivity.this,"你点击了确定按钮",Toast.LENGTH_SHORT).show();
}
});
//对话框中绑定取消按钮
builder.setNegativeButton("取消",null);
//生产Dialog
//调用Builder对象的create()方法创建AlertDialog对象
AlertDialog dialog = builder.create();
//调用AlertDialog对象的show()方法将对话框显示出来
dialog.show();
}
//列表对话框
//构建数据
String[] citys = {"北京","上海","广州","中山","珠海"};//数组
public void test_2(View view){
//当点击选择城市输入框时,在当前Activity中构建一个对话框Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框标题
builder.setTitle("选择城市");
//设置对话框图标
builder.setIcon(R.mipmap.ic_launcher);
//设置列表的项,传入citys输入,new一个点击监听事件的方法
builder.setItems(citys,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface,int i){
//当点击了某一项后,弹出提示,你选择了某个城市
Toast.makeText(MainActivity.this,"你选择了"+citys[i],Toast.LENGTH_SHORT).show();
//不仅将选择的内容弹出来,还要将内容绑定到输入框中
ucity.setText(citys[i]);
ucity.setSelection(citys[i].length());
}
});
builder.create().show();
}
//单选提示框
int selectedIndex = -1;
public void test_3(View view){
//同上构建builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//标题
builder.setTitle("请选择城市");
//图标
builder.setIcon(R.mipmap.ic_launcher);
//设置单选按钮,传入citys数据,1,表示默认选中第二项
builder.setSingleChoiceItems(citys,1,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface,int i){
selectedIndex = i;//记录被选中的下标
}
});
//绑定按钮
builder.setPositiveButton("确认",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface,int i){
if (selectedIndex>=0){
//当点击确认按钮后弹出选择的内容
Toast.makeText(MainActivity.this,"你选择了:"+citys[selectedIndex],Toast.LENGTH_SHORT).show();
}
}
});
//绑定取消按钮
builder.setNegativeButton("取消",null);
builder.create().show();
}
//多选提示框
//默认多选按钮的每一项都未被选中
boolean[] selected = {false,false,false,false,false};
public void test_4(View view){
//同上
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择城市");
builder.setIcon(R.mipmap.ic_launcher);
//设置为复选框,传入citys数据,selected表示多选按钮的状态都为false
builder.setMultiChoiceItems(citys,selected,new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialogInterface,int i,boolean b){
if (b){
//如果项被选中,则将该项的值设置为b,b代表true;
selected[i] = b;
}
}
});
builder.setPositiveButton("确认",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface,int i){
//创建缓存
StringBuffer sb = new StringBuffer();
//通过循环遍历将所有为true选项的citys中的数据追加到缓存中
for (int j=0;j<selected.length;j++){
if (selected[j]){
sb.append(citys[j]+",");
}
}
//判断缓存中是否有值
if (sb.length()>0){
//如果有值则将缓存中的值进行截取字符串,减1是为了除去最后一个逗号
String newStr = sb.substring(0,sb.length() - 1);
//然后将截取到的字符串弹出来
Toast.makeText(MainActivity.this,"你选择了:"+newStr,Toast.LENGTH_SHORT).show();
}
}
});
//设置取消按钮
builder.setNegativeButton("取消",null);
builder.create().show();
}
//日期对话框
@TargetApi(Build.VERSION_CODES.N)
public void test_5(View view){
//构建一个日历对象
Calendar cal = Calendar.getInstance();//当前日期
//2个月后的今天日期是多少
cal.add(Calendar.MONTH,2);
//创建日历对话框,设置选择日期的监听事件的方法
DatePickerDialog dialog = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {
//参数i表示数字年,参数i1表示数字月,参数i2表示数字日
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
//给ui组件绑定选中的日期
intime.setText(i + "-" + (i1 + 1) + "-" + i2);
intime.setSelection(intime.getText().length());
}
},cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DATE)+1);
//显示日历对话框
dialog.show();
}
//时间对话框
@TargetApi(Build.VERSION_CODES.N)
public void test_6(View view){
//构建一个日历对象
Calendar cal = Calendar.getInstance();//当前日期
//在当前Activity中构建一个时间对象,并监听选择的时间
TimePickerDialog dialog = new TimePickerDialog(this,new TimePickerDialog.OnTimeSetListener() {
//参数i表示数字小时,参数i1表示数字分钟
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
//设置时间显示的字符串格式
String time = i + ":" + i1;
//将时间绑定到UI输入框中
workTime.setText(time);
}
},cal.get(Calendar.HOUR)+1,cal.get(Calendar.MINUTE),true);
dialog.show();
}
//定义进度条对话框
ProgressDialog progressDialog;
boolean isRun = true;
int i = 1;
public void test_7(View view){
i=1;
//当点击按钮后就进行下载
isRun = true;
//在当前Activity中实例化一个进度条对话框
progressDialog = new ProgressDialog(this);
//设置标题
progressDialog.setTitle("下载");
//设置图标
progressDialog.setIcon(R.mipmap.ic_launcher);
progressDialog.setMessage("文件正在下载中....."); //控制进度条样式风格,这里是条形风格,还可以设置圆圈分隔等
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置一个按钮,当点击取消下载
progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE,"取消下载",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface,int i){
//则停止进度
isRun = false;
//并将进度重新设置为0
i=0;
}
});
//显示进度条对话框
progressDialog.show();
//开始执行下载操作
new Thread(new Runnable() {
@Override
public void run() {
while (isRun){
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setMessage("文件已下载"+i+"%");
}
});
//设置进度
progressDialog.setProgress(i);
SystemClock.sleep(100);
i++;
//如果i大于100,则停止下载,并提示文件下载完毕
if (i>100){
isRun = false;
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setMessage("文件已下载完毕");
}
});
//休眠1秒,关闭对话框
SystemClock.sleep(1000);
progressDialog.dismiss();
}
}
}
}).start();
}
//自定义对话框
private EditText uname,upwd;
public void test_8(View view){
//获取LayoutInflater对象
LayoutInflater inflater = LayoutInflater.from(this);
//通过投影加载自定义Dialog布局View
View layout = inflater.inflate(R.layout.layout_dialog_login,null);
uname = (EditText)layout.findViewById(R.id.uname);
upwd = (EditText)layout.findViewById(R.id.upwd);
//构建对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("用户登录");
builder.setIcon(R.mipmap.ic_launcher);
//设置View,将投影加载到的布局绑定到对话框
builder.setView(layout);
//设置button
builder.setPositiveButton("登录",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface,int i){
//当点击登录按钮后,获取用户名和密码输入框中的文本,转化为字符串
String name = uname.getText().toString();
String pwd = upwd.getText().toString();
//弹出信息
Toast.makeText(MainActivity.this,"用户名:"+name+",密码:"+pwd,Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("注册",null);
builder.setNeutralButton("取消",null);
builder.create().show();
}
}
3、layout_dialog_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="15sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/uname"
android:hint="请输入用户名"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="15sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/upwd"
android:inputType="textPassword"
android:hint="请输入密码"
/>
</LinearLayout>
Android开发 ---构建对话框Builder对象,消息提示框、列表对话框、单选提示框、多选提示框、日期/时间对话框、进度条对话框、自定义对话框、投影的更多相关文章
- Android开发学习之路--异步消息Handler,Message,Looper和AsyncTask之初体验
在简易音乐播放器中,用了Handler,也没有过多地去研究学习,这里再学习下android下的异步消息处理机制.这里用了Handler主要是在线程中不能更新UI,而需要通过Handler才可以.关于异 ...
- Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关
Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个按钮 <?xml version="1.0" encoding=" ...
- Android开发——构建自定义组件
Android中,你的应用程序程序与View类组件有着一种固定的联系,例如按钮(Button). 文本框(TextView), 可编辑文本框(EditText), 列表框(ListView), 复选框 ...
- Android开发——使用intent传递对象
intent传递对象有两种方法: 方式一:Serializable 方式 方式二:Parcelable方式 在这里不多介绍了,这一篇就是快速上手使用教程,至于详细原理介绍的,请看这一篇http://w ...
- Android开发学习之路-Handler消息派发机制源码分析
注:这里只是说一下sendmessage的一个过程,post就类似的 如果我们需要发送消息,会调用sendMessage方法 public final boolean sendMessage(Mess ...
- Android开发 - 下拉刷新和分段头悬停列表
项目源码 本文所述项目已开源,源码地址 为什么做PullToRefresh-PinnedSection-ListView 前段时间因为项目需求,需要在Android中对ListView同时增加下拉刷新 ...
- Android开发之使用BaseAdapter的notifyDataSetChanged()无法更新列表
在做一个通讯录的app,使用BaseAdapter作为adapter.重写了getCount().getItem().getItemId() .getView()方法. 因为新建联系人在第二个acti ...
- Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)
Android Studio常见对话框(普通对话框.单选对话框.多选对话框.进度条对话框.消息对话框.自定义对话框) 1.普通对话框 2.单选对话框 3.多选对话框 4.进度条对话框 5.消息对话框 ...
- Android开发学习笔记-自定义对话框
系统默认的对话框只能显示简单的标题内容以及按钮,而如果想要多现实其他内容则就需要自定义对话框,下面是自定义对话框的方法. 1.先定义对话框的模版 <?xml version="1.0& ...
随机推荐
- sed command
https://blog.csdn.net/solaraceboy/article/details/79272344
- [openjudge-搜索]湖的深度
题目描述 描述 一个湖用 R x C (1 ≤ R ≤ 50; 1 ≤ C ≤ 50) 的网格表示.格点上的非负整数 D_rc (0 ≤ D_rc ≤ 1,000,000)表示该位置的深度.整数0表示 ...
- Spring Boot 的 application.properties
更改默认端口:8080 server.port = 8081 更改context-path :/server.context-path = /springboot #server.address= # ...
- <转>jmeter(二十一)jmeter常用插件介绍
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...
- 使用shiro框架,注销问题的解决
在使用shiro框架的时候,有时候会因为登录问题找不到注销的controller.所以会报404的错误,下面是解决办法: 1.首先写一个类SystemLogoutFilter继承LogoutFilte ...
- Spring笔记 #02# 利用切面和注解校验方法参数
例子还是之前的例子.仍然是对mage进行法术攻击时的咒语进行校验,不过略微提高了扩展性. 应用示例 1.在.properties文件中定义参数格式(正则): sp1=^\\D*hello\\D*$ s ...
- topcoder srm 580 div1
problem1 link 最优选择一定是在$2n$个端点中选出两个. problem2 link 分开考虑每个区间.设所有区间的左端点的最大值为$lc$,所有区间的右端点的最小值为$rc$.对于某个 ...
- C#方法过滤器
方法过滤器 使用Emit和注解属性Attribute实现 使用方式 1. 自定义方法过滤器 可分别定义方法执行前过滤器, 方法执行结束过滤器, 方法异常过滤器 执行前过滤器继承 ExecutingFi ...
- bzoj1001狼抓兔子 对偶图优化
bzoj1001狼抓兔子 对偶图优化 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路 菜鸡总是要填坑的! 很明显让你求网格图的最 ...
- 论文笔记: Mutual Learning to Adapt for Joint Human Parsing and Pose Estimation
Mutual Learning to Adapt for Joint Human Parsing and Pose Estimation 2018-11-03 09:58:58 Paper: http ...