对话框控件—Dialog

对话框(Dialog)也是Android系统中常用的用户界面元素,它的直接子类是AlertDialog,间接子类有DatePickerDialog、ProgressDialog和TimePickerDialog。这一节我们介绍它们的基本用法。

主界面testdialog.xml

1.   <?xml version="1.0" encoding="utf-8"?> 

2.   <LinearLayout

3.   xmlns:android="http://schemas.android.com/apk/res/android"

4.   android:layout_width="fill_parent"

5.   android:layout_height="fill_parent"

6.   android:orientation="vertical"

7.   android:background="#ffe8e8e8">

8.   <Button android:id="@+id/button1"

9.   android:layout_height="wrap_content"

10.  android:layout_width="fill_parent"

11.  android:text="AlertDialog"/>

12.  <Button android:id="@+id/button2"

13.  android:layout_height="wrap_content"

14.  android:layout_width="fill_parent"

15.  android:text="DatePickerDialog"/>

16.  <Button android:id="@+id/button3"

17.  android:layout_height="wrap_content"

18.  android:layout_width="fill_parent"

19.  android:text="TimePickerDialog"/>

20.  <Button android:id="@+id/button4"

21.  android:layout_height="wrap_content"

22.  android:layout_width="fill_parent"

23.  android:text="ProgressDialog"/>

24.  </LinearLayout>

主界面用了纵向的线性布局,放了4个普通按钮,如图10-9所示。

 

▲图10-9  对话框示例界面图

代码文件Testdialog.java:

1.   public class Testdialog  extends Activity{

2.   private Button button1,button2,button3,button4;

3.   final int MyAlertDialog = 1,MyDatePickerDialog=2,

4.   MyTimePickerDialog=3,MyProgressDialog=4;

5.   Calendar dateAndTime = Calendar.getInstance();

6.   @Override

7.   public void onCreate(Bundle savedInstanceState) {

8.   super.onCreate(savedInstanceState);

9.   setContentView(R.layout.testdialog);

10.  findwidget();

11.  }

12.  void findwidget()

13.  {

14.  button1=(Button)findViewById(R.id.button1);

15.  button2=(Button)findViewById(R.id.button2);

16.  button3=(Button)findViewById(R.id.button3);

17.  button4=(Button)findViewById(R.id.button4);

18.  button1.setOnClickListener(Btocl);

19.  button2.setOnClickListener(Btocl);

20.  button3.setOnClickListener(Btocl);

21.  button4.setOnClickListener(Btocl);

22.  }

23.  View.OnClickListener Btocl= new View.OnClickListener() {

24.  @Override

25.  public void onClick(View v) {

26.  switch (v.getId())

27.  {

28.  case R.id.button1:

29.  showDialog(MyAlertDialog);

30.  break;

31.  case R.id.button2:

32.  showDialog(MyDatePickerDialog);

33.  break;

34.  case R.id.button3:

35.  showDialog(MyTimePickerDialog);

36.  break;

37.  case R.id.button4:

38.  showDialog(MyProgressDialog);

39.  break;  

40.  }}};

41.  @Override

42.  public Dialog onCreateDialog(int id)

43.  {

44.  switch (id) {

45.  case MyAlertDialog:

46.  Dialog dialog =new AlertDialog.Builder(this)

47.  .setIcon(R.drawable.ic_launcher).setTitle("MY对话框")

48.  .setMessage("一个自己设置的对话框哦,好看不?")

49.  .setNegativeButton("不好看", ocl).setNeutralButton("一般般", ocl)

50.  .setPositiveButton("很喜欢", ocl).create();

51.  return dialog ;

52.  case MyDatePickerDialog:

53.  DatePickerDialog dateDatePickerDialog=

54.  new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {

55.  @Override

56.  public void onDateSet(DatePicker view, int year, int monthOfYear,

57.  int dayOfMonth) {

58.  }},dateAndTime.get(Calendar.YEAR),dateAndTime.get(Calendar.MONTH),dateAndTime.get
     (Calendar.DAY_OF_MONTH));

59.  return dateDatePickerDialog;

60.  case MyTimePickerDialog:

61.  TimePickerDialog timePickerDialog=new TimePickerDialog(this,new TimePickerDialog.
    OnTimeSetListener() {

62.  @Override

63.  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

64.  }   } ,

    dateAndTime.get(Calendar.HOUR_OF_DAY),dateAndTime.get(Calendar.MINUTE),  true);

65.  return timePickerDialog;

66.  case MyProgressDialog:

67.  ProgressDialog progressDialog=new ProgressDialog(this);

68.  progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

69.  progressDialog.setMessage("Loading...");

70.  progressDialog.setCancelable(true);

71.  return progressDialog ;}

72.  return null;}

73.  OnClickListener ocl = new OnClickListener() {

74.  @Override

75.  public void onClick(DialogInterface dialog, int which) {

76.  switch (which) {

77.  case Dialog.BUTTON_NEGATIVE:

78.  break;

79.  case Dialog.BUTTON_NEUTRAL:

80.  break;

81.  case Dialog.BUTTON_POSITIVE:

82.  break;

83.  }}};

84.  }

第2行我们定义了4个button按钮,用来单击触发4种对话框。

第3~4行定义了4个整数常量,这些常量作为4个对话框的唯一标识用来在创建、显示、取消等操作时系统标识用。

第5行定义的Calendar对象dateAndTime用来在日期和时间对话框时获取系统日期时间。

第14~22行我们实例化了4个按钮对象,并设置了统一的按钮单击监听器Btocl。

第23~40行是View.OnClickListener对象Btocl,这个回调函数在用户单击按钮时触发onClick函数,并传入单击按钮的View。通过v.getId()我们获取到按钮的ID号,然后4个按钮各自执行showDialog函数,调用创建对话框。

当使用showDialog(int id) 函数方式创建对话框时,如果此ID对应的对话框对象是第一次被请求时,Android系统就回调Activity中的onCreateDialog(int id)函数,我们在onCreateDialog函数里进行对话框对象创建。

第41~71行是具体创建对话框对象的onCreateDialog函数,在这里我们使用一个switch 语句根据传入的id 参数初始化对应对话框对象。当创建完对话框后,返回这个对象给activity进行管理。

第46~51行是创建AlertDialog对话框。AlertDialog对话框不能直接new方式创建,必须先创建AlertDialog.Builder对象,然后调用它的create方法来创建AlertDialog。

AlertDialog对话框对象能显示一个图标、一个标题、一个内容文本和3个可选按钮。

第47行设置了AlertDialog的图标和标题,第48行设置了内容,第49行设置了NegativeButton按钮和NeutralButton按钮,第50行设置了PositiveButton按钮。虽然这3个按钮名字不同,但在功能上是可以随意设定的,只是为了取ID方便。

AlertDialog对话框的OnClickListener按钮监听器ocl在第73~82行定义,我们通过onClick函数传入的which值确定哪个按钮被单击了并可设定相应处理代码。

下面看一下我们这个AlertDialog的示例图。

第52~59行是日期选择对话框DatePickerDialog。我们直接使用new的方式产生对象。DatePickerDialog构造函数需要传入:context上下文、DatePickerDialog.OnDateSetListener()监听器和初始化年月日。DatePickerDialog.OnDateSetListener()监听器在用户单击设置按钮时回调onDateSet函数,反馈用户设置的年月日。

图10-11是日期选择对话框的示例图。

第60~65行创建时间选择对话框TimePickerDialog的方式和日期选择对话框类似。通过TimePickerDialog构造函数传入上下文、监听器和初始化的时间值,并设置是否是24小时制。监听器TimePickerDialog.OnTimeSetListener()里onTimeSet函数在用户按了设置按钮后传入设置后的时间值。

图10-12是时间选择对话框的示例图。

         

▲图10-11  日期选择对话框示例图                  ▲图10-12  时间选择对话框示例图

第66~72行是进度对话框ProgressDialog。同样可以直接new对象。

第68行设置进度条显示风格,在此我们采用系统自带的ProgressDialog.STYLE_SPINNER。系统自带了好几种风格,读者可以课后试验一下各种风格。

第69行设置显示的提示信息。

第71行设定了我们能通过按设备的back键取消这个对话框,如果设置为false则只能通过程序代码取消。

进度对话框示例如图10-13所示。

ProgressDialog是AlertDialog的扩展类,也能设置按钮,比如一个取消下载的按钮。但与AlertDialog.Builder不同,ProgressDialog是调用setButton,setButton2,setButton3函数来创建按钮。

对于创建完的对话框对象,我们可以通过调用该对象dismiss()来消除它,但我们推荐在Activity中调用dismissDialog(int id)的方式取消。如果不再需要对话框对象时,可以调用removeDialog(int id)来删除。

posted @ 2015-03-03 17:53  菜鸟的学习  阅读(1327)  评论(0编辑  收藏  举报