Dialog弹出对话框
常见的有六种对话框
这里需要补充实现单击事件的三种方式
1.在布局中添加onclick属性
2.在activity中实现View.OnClickListener的接口,在实现其onclick()方法,切记要绑定监事件适合用于按钮很多的时候
简单消息框
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
builder.setTitle("温馨提示");
builder.setMessage("你有一个新的消息");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "确定退出", Toast.LENGTH_SHORT).show();
}
});
builder.show();
弹出消息框初始化AlertDialog.Builder控件,其中的参数为·当前activity.this
设置对话框的标题用builder.setTitle()
设置对话框的消息提示用builder.setMessage()
此时需要弹出一个消息框
在里面调用Toast
自定义消息框
自定义消息框需要自定义的布局文件,所以自定义的第一步就是把布局加进去
自定义布局文件
<?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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="用户名"
android:gravity="center"
android:layout_width="80dp"
android:layout_height="40dp"
android:textSize="20dp"/>
<EditText
android:layout_width="200dp"
android:layout_height="40dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="密 码"
android:gravity="center"
android:layout_width="80dp"
android:layout_height="40dp"
android:textSize="20dp"/>
<EditText
android:layout_width="200dp"
android:layout_height="40dp"
/>
</LinearLayout>
</LinearLayout>
接着是activity当中实现自定义对话框的代码
View view = LayoutInflater.from(this).inflate(R.layout.my, null);
et_name = (EditText) view.findViewById(R.id.et_name);
et_pwd = (EditText) view.findViewById(R.id.et_pwd);
AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
builder1.setTitle("温馨提示");
builder1.setMessage("真的要退出吗");
builder1.setIcon(R.mipmap.ic_launcher);
builder1.setView(view);
builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "确定哦", Toast.LENGTH_SHORT).show();
}
});
builder1.show();
首先要把自定义的布局文件加进来用View
调用ayoutInflater.from(this).inflate(R.layout.my, null);方法,其中。inflate中的第一个参数就是所需要加进去布局文件,第二个参数填null;
接着初始化控件Edit Text。要用View.find viewByid(),因为上面已经初始化view了,不能直接绑定id,要通过view
设置对话框的图片要用 builder1.setIcon()方法
需要注意的是这里一定要·调用 builder1.setView(view)方法,把布局和view加载进来
圆形对话框
pd = new ProgressDialog(DialogActivity.this);
pd.setTitle("提示");
pd.setMessage("这是一个圆形对话框");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setProgress(100);
pd.setButton(-1, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
pd.show();
进度条对话框要用ProgressDialog控件
设置进度条样式 pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);这里为圆形进度条样式
设置按钮 pd.setButton()其中有三个参数:
第一个参数为-1,0,1;代表积极的,中性的,消极的()
第二个参数:”确定”,”取消”
第三个参数: new DialogInterface.OnClickListener()
进度条对话框
pd = new ProgressDialog(DialogActivity.this);
pd.setTitle("");
pd.setMessage("");
pd.setMax(100);
pd.setCancelable(false);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
pd.setProgress(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
设置单击其他的地方是否消息框会消失 pd.setCancelable(false);
设置进度条的最大值 pd.setMax(100);
设置进度条样式 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
因为·是进度条所以一直转有数字显示,此处应该加线程
日历对话框
Calendar cal = Calendar.getInstance();
DatePickerDialog dp = new DatePickerDialog(DialogActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(DialogActivity.this, "所选择的日期是" + year + "-" + month + 1 + "-" + dayOfMonth, Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dp.show();
初始化 Calendar 调用 Calendar.getInstance();方法初始化
日期应该用DatePickerDialog控件,初始化DatePickerDialog
里面有两个参数:当前activity.this,new DatePickerDialog.OnDateSetListener()
应该在括号当中调用下面三个方法,否则·会一直报错
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)
##时间对话框
Calendar cal1 = Calendar.getInstance();
TimePickerDialog tp = new TimePickerDialog(DialogActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(DialogActivity.this, "", Toast.LENGTH_SHORT).show();
}
}, 11, cal1.get(Calendar.MINUTE), true);
tp.show();
初始化 Calendar 调用 Calendar.getInstance();方法初始化
时间应该用TimePickerDialog 控件,初始化TimePickerDialog
里面有两个参数:当前activity.this,new DatePickerDialog.OnDateSetListener()
应该在括号当中调用下面三个方法,否则·会一直报错
, 11, cal1.get(Calendar.MINUTE), true