对话框式程序运行中弹出的窗口。Android系统中有四种默认的对话框:警告对话框AlertDialog、进度对话框ProgressDialog、日期选择对话框DatePickerDialog以及时间选择对话框TimePickerDialog。除此之外,我们自定义自已的dialog。
一. 警告对话框(AlertDialog)
Android系统中最常用的对话框是AlertDialog,它是一个提示窗口,需要用户作出选择的。一般会有几个按钮、标题信息、提示信息等。
在程序中创建AlertDialog的步骤:
1.获得AlertDialog的静态内部类Builder对象,由该类来创建对话框,Builder所提供的方法如下:
setTitle():给对话框设置title.
setIcon():给对话框设置图标。
setMessage():设置对话框的提示信息
setItems():设置对话框要显示的一个list,一般用于要显示几个命令时
setSingleChoiceItems():设置对话框显示一个单选的List
setMultiChoiceItems():用来设置对话框显示一系列的复选框。
setPositiveButton():给对话框添加”Yes”按钮。
setNegativeButton():给对话框添加”No”按钮。
2.调用Builder的create( )方法
3.调用AlertDialog的show( )方法显示对话框
下面是一个提示信息对话框的实例:
AlertDialogActivity.java
- package com.android.dialog.activity;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class AlertDialogActivity extends Activity {
- private TextView tv;
- private Button btn;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tv = (TextView)findViewById(R.id.TextView_1);
- btn = (Button)findViewById(R.id.Button_1);
- //实例化AlertDialog.Builder对象
- final AlertDialog.Builder builder = new AlertDialog.Builder(this);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- //设置提示信息,确定按钮
- builder.setMessage("真的要删除该文件吗?").setPositiveButton("是", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- tv.setText("成功删除");
- }
- //设置取消按钮
- }).setNegativeButton("否", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- tv.setText("取消删除");
- }
- });
- //创建对话框
- AlertDialog ad = builder.create();
- //显示对话框
- ad.show();
- }
- }
- );
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/TextView_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="测试AlertDialog"
- />
- <Button
- android:id="@+id/Button_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="删除文件"
- />
- </LinearLayout>
效果图:
二.进度对话框(ProgressDialog)
在程序中创建ProgressDialog的步骤:
1.覆盖Activity的onCreateDialog( )方法,并在其中创建对话框
2.调用Activity的showDialog( )方法,显示进度对话框
下面是一个提示进度对话框的实例:
ProgressDialogActivity.java
- package com.android.progress.activity;
- import android.app.Activity;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class ProgressDialogActivity extends Activity {
- private Button btn;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn = (Button)findViewById(R.id.Button_1);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- //调用Activity的showDialog()方法,显示进度对话框
- showDialog(0);
- }
- });
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- //对进度对话框进行实例化
- ProgressDialog dialog = new ProgressDialog(this);
- //设置显示的标题
- dialog.setTitle("测试ProgressDialog");
- dialog.setIndeterminate(true);
- dialog.setMessage("程序正在Loading...");
- dialog.setCancelable(true);
- dialog.setButton(Dialog.BUTTON_POSITIVE, "确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- }
- );
- return dialog;
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/Button_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="测试ProgressDialog"
- />
- </LinearLayout>
效果图:
三.日期,时间选择对话框(DatePickerDialog、TimePickerDialog)
在程序中创建日期,时间选择对话框的步骤:
1.覆盖Activity的onCreateDialog( )方法,并在其中创建对话框
2.分别在OnDateSetListener的onDateSet( )方法和OnTimeSetListener( )的onTimeSet( )事件方法中更改日期,时间
3.调用Activity的showDialog( )方法,显示进度对话框
MainActivity.java
- package com.android.datatime.activity;
- import java.util.Calendar;
- import android.app.Activity;
- import android.app.DatePickerDialog;
- import android.app.Dialog;
- import android.app.TimePickerDialog;
- import android.app.DatePickerDialog.OnDateSetListener;
- import android.app.TimePickerDialog.OnTimeSetListener;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.DatePicker;
- import android.widget.TextView;
- import android.widget.TimePicker;
- public class MainActivity extends Activity {
- private Button btn1,btn2;
- private TextView tv_1,tv_2;
- private Calendar c;//获得日历实例
- private int m_year,m_month,m_day;
- private int m_hour,m_minute;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn1 = (Button)findViewById(R.id.Button_1);
- btn2 = (Button)findViewById(R.id.Button_2);
- c = Calendar.getInstance();
- m_year = c.get(Calendar.YEAR);
- m_month = c.get(Calendar.MONTH);
- m_day = c.get(Calendar.DAY_OF_MONTH);
- m_hour = c.get(Calendar.HOUR);
- m_minute = c.get(Calendar.MINUTE);
- tv_1 = (TextView)findViewById(R.id.TextView_1);
- tv_1.setText(m_year+":"+(m_month+1)+":"+m_day);//设置TextView里的内容为日期
- tv_2 = (TextView)findViewById(R.id.TextView_2);
- tv_2.setText(m_hour+":"+m_minute);//设置TextView里的内容为时间
- btn1.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- showDialog(0);//显示日期对话框
- }
- });
- btn2.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- showDialog(1);//显示时间对话框
- }
- });
- }
- //调用Activity的showDialog( )方法显示进对话框
- protected Dialog onCreateDialog(int id) {
- if(id==0)
- return new DatePickerDialog(this,l1,m_year, m_month, m_day);
- else
- return new TimePickerDialog(this, l2, m_hour, m_minute, false);
- }
- //设置日期监听器
- private OnDateSetListener l1 = new OnDateSetListener() {
- public void onDateSet(DatePicker view, int year, int monthOfYear,
- int dayOfMonth) {
- m_year = year;
- m_month = monthOfYear;
- m_day = dayOfMonth;
- tv_1.setText(m_year+":"+(m_month+1)+":"+m_day);//为TextView设置文本内容,重新显示日期
- }
- };
- //设置时间监听器
- private OnTimeSetListener l2 = new OnTimeSetListener() {
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
- m_hour = hourOfDay;
- m_minute = minute;
- tv_2.setText(m_hour+":"+m_minute);//为TextView设置文本内容,重新显示时间
- }
- };
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:text=""
- android:id="@+id/TextView_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <TextView
- android:text=""
- android:id="@+id/TextView_2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <Button
- android:text="修改日期"
- android:id="@+id/Button_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <Button
- android:text="修改时间"
- android:id="@+id/Button_2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
效果图:
转载于:https://blog.51cto.com/liangruijun/641365