对话框 AlertDialog

来源:互联网 发布:手机内存清理软件 编辑:程序博客网 时间:2024/04/29 07:31
介绍一下Android基本组件:对话框AlertDialog。

API:

java.lang.Object↳android.app.AlertDialog.Builder

使用AlertDialog.Builder创建对话框需要了解以下几个方法:

  • setTitle :为对话框设置标题
  • setIcon :为对话框设置图标
  • setMessage:为对话框设置内容
  • setView : 给对话框设置自定义样式
  • setItems :设置对话框要显示的一个list,一般用于显示几个命令时。
  • setMultiChoiceItems :用来设置对话框显示一系列的复选框。
  • setNeutralButton :
  • setPositiveButton :给对话框添加"Yes"按钮
  • setNegativeButton :对话框添加"No"按钮
  • create : 创建对话框
  • show :显示对话框

下面我们来看一下最简单对话框。


这个对话框只是简单的显示内容,使用默认图标,没有按钮,不多说,贴代码:


[java] view plaincopyprint?
  1. new AlertDialog.Builder(Lesson_01_Pic.this).setTitle("提示标题").setMessage("这是提示内容").show();

(Lesson_02_Dia是类名,请换成自己的!!)

下面我们为这个对话框加个按钮,效果:

代码:

[java] view plaincopyprint?
  1. new AlertDialog.Builder(Lesson_01_Pic.this)
  2. .setTitle("这是标题")
  3. .setMessage("这是提示内容")
  4. .setPositiveButton("确定",
  5. new DialogInterface.OnClickListener(){
  6. public void onClick(DialogInterface dialoginterface,int i){
  7. //按钮事件
  8. Toast.makeText(Lesson_01_Pic.this,"确定",Toast.LENGTH_LONG).show();
  9. }
  10. }).show();

添加按钮时,需要同时为该按钮指定监听器。

下面,我们修改一个图标,添加两个按钮,同时显示多个选项,先看下效果:

代码:

[c-sharp] view plaincopyprint?
  1. package com.yfz;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.app.Dialog;
  5. import android.content.DialogInterface;
  6. import android.content.DialogInterface.OnClickListener;
  7. import android.content.DialogInterface.OnMultiChoiceClickListener;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.Toast;
  12. public class Lesson_02_Dia extends Activity {
  13. /** Called when the activity is first created. */
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. Button button = (Button)findViewById(R.id.b01);
  19. button.setText("对话框");
  20. button.setOnClickListener(new Button.OnClickListener(){
  21. @Override
  22. public void onClick(View v) {
  23. //选项数组
  24. String[] choices={"Facebook","Twitter"};
  25. //Check判断数组,与选项对应
  26. boolean[] chsBool = {true,false};
  27. //包含多个选项及复选框的对话框
  28. AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this)
  29. .setIcon(android.R.drawable.btn_star_big_on)
  30. .setTitle("调查")
  31. .setMultiChoiceItems(choices, chsBool, multiClick)
  32. .setPositiveButton("Yes", onclick)
  33. .setNegativeButton("No", onclick).create();
  34. dialog.show();
  35. }
  36. });
  37. }
  38. /**
  39. * 对话框复选框事件监听器
  40. */
  41. OnMultiChoiceClickListener multiClick = new OnMultiChoiceClickListener(){
  42. @Override
  43. public void onClick(DialogInterface dialog,int which, boolean isChecked) {
  44. Toast.makeText(Lesson_02_Dia.this,"第"+(which+1)+"项,选中结果:"+isChecked,Toast.LENGTH_SHORT).show();
  45. }
  46. };
  47. /**
  48. * 对话框按钮点击事件监听器
  49. */
  50. OnClickListener onclick = new OnClickListener() {
  51. @Override
  52. public void onClick(DialogInterface dialog,int which) {
  53. switch (which) {
  54. case Dialog.BUTTON_NEGATIVE:
  55. Toast.makeText(Lesson_02_Dia.this,"No..",
  56. Toast.LENGTH_LONG).show();
  57. break;
  58. case Dialog.BUTTON_NEUTRAL:
  59. Toast.makeText(Lesson_02_Dia.this,"I don't know.",
  60. Toast.LENGTH_LONG).show();
  61. break;
  62. case Dialog.BUTTON_POSITIVE:
  63. Toast.makeText(Lesson_02_Dia.this,"Yes!!",
  64. Toast.LENGTH_LONG).show();
  65. break;
  66. }
  67. }
  68. };
  69. }

说明已经写在注释中了。

下面再介绍一种比较常用的式样,如图:

代码:

[java] view plaincopyprint?
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.main);
  5. Button button = (Button)findViewById(R.id.b01);
  6. button.setText("对话框");
  7. button.setOnClickListener(new Button.OnClickListener(){
  8. @Override
  9. public void onClick(View v) {
  10. //选项数组
  11. String[] choices={"新浪微博","校内","街旁"};
  12. //包含多个选项的对话框
  13. AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this)
  14. .setIcon(android.R.drawable.btn_star)
  15. .setTitle("分享")
  16. .setItems(choices, onselect).create();
  17. dialog.show();
  18. }
  19. });
  20. }
  21. /**
  22. * 选项的事件监听器
  23. */
  24. OnClickListener onselect = new OnClickListener() {
  25. @Override
  26. public void onClick(DialogInterface dialog,int which) {
  27. // TODO Auto-generated method stub
  28. switch (which) {
  29. case 0:
  30. Toast.makeText(Lesson_02_Dia.this,"您选择了新浪微博.",Toast.LENGTH_SHORT).show();
  31. break;
  32. case 1:
  33. Toast.makeText(Lesson_02_Dia.this,"您选择了校内",Toast.LENGTH_SHORT).show();
  34. break;
  35. case 2:
  36. Toast.makeText(Lesson_02_Dia.this,"您选择了街旁",Toast.LENGTH_SHORT).show();
  37. break;
  38. }
  39. }
  40. };

好了,今天就写到这,改天写一下,如果在弹出框中做一个登陆界面。

继续补充...先上图...

页面login.xml: 示例写的比较简单,布局大家可以自己完善、修改。

[xhtml] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TableLayout
  3. android:id="@+id/widget36"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical"
  7. xmlns:android="http://schemas.android.com/apk/res/android"
  8. >
  9. <TextView
  10. android:id="@+id/widget37"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="用户名:"
  14. >
  15. </TextView>
  16. <EditText
  17. android:id="@+id/widget38"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text=""
  21. android:textSize="18sp"
  22. >
  23. </EditText>
  24. <TextView
  25. android:id="@+id/widget39"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="密码:"
  29. >
  30. </TextView>
  31. <EditText
  32. android:id="@+id/widget40"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text=""
  36. android:textSize="18sp"
  37. >
  38. </EditText>
  39. </TableLayout>

代码 : (也比较简单)

[c-sharp] view plaincopyprint?
  1. LayoutInflater factory = LayoutInflater.from(Lesson_02_Dia.this);
  2. //获得自定义对话框
  3. View view = factory.inflate(R.layout.login, null);
  4. AlertDialog dialog02 = new AlertDialog.Builder(Lesson_02_Dia.this)
  5. .setIcon(android.R.drawable.btn_star)
  6. .setTitle("登录")
  7. .setView(view)
  8. .setPositiveButton("Yes", onclick)
  9. .setNegativeButton("No", onclick).create();
  10. dialog02.show();

原创粉丝点击