Android下的对话框
layout文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:onClick="click0" 11 android:text="开启对话框activities" /> 12 13 <Button 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:onClick="click1" 17 android:text="显示通知对话框" /> 18 19 <Button 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:onClick="click2" 23 android:text="显示单选对话框" /> 24 25 <Button 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" 28 android:onClick="click3" 29 android:text="显示带按钮的单选对话框" /> 30 31 <Button 32 android:layout_width="fill_parent" 33 android:layout_height="wrap_content" 34 android:onClick="click4" 35 android:text="显示带按钮的多选对话框" /> 36 <Button 37 android:layout_width="fill_parent" 38 android:layout_height="wrap_content" 39 android:onClick="click5" 40 android:text="进度条对话框" /> 41 <Button 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:onClick="click6" 45 android:text="进度条对话框显示具体进度" /> 46 </LinearLayout>
Activity
DemoActivity.java
1 package cn.itcast.dialog; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.app.AlertDialog.Builder; 6 import android.app.ProgressDialog; 7 import android.content.DialogInterface; 8 import android.content.DialogInterface.OnClickListener; 9 import android.content.DialogInterface.OnMultiChoiceClickListener; 10 import android.content.Intent; 11 import android.net.Uri; 12 import android.os.Bundle; 13 import android.view.View; 14 import android.widget.Toast; 15 16 public class DemoActivity extends Activity { 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.main); 21 } 22 23 // 当当前的activity失去焦点的时候调用的方法. 24 @Override 25 protected void onPause() { 26 // 对话框 是activity的一部分,弹出对话框 不会让activity失去焦点. 27 System.out.println("失去焦点"); 28 super.onPause(); 29 } 30 31 public void click0(View view) { 32 Intent intent = new Intent(this, DialogActivity.class); 33 startActivity(intent); 34 } 35 36 public void click1(View view) {// 显示一个通知对话框 37 // 创建一个对话框的构建者 38 AlertDialog.Builder builder = new Builder(this); 39 builder.setIcon(R.drawable.ic_launcher); 40 builder.setTitle("我是对话框标题"); 41 builder.setMessage("打开黑马网站"); 42 builder.setPositiveButton("确定", new OnClickListener() { 43 44 public void onClick(DialogInterface dialog, int which) { 45 Intent intent = new Intent(); 46 intent.setAction(Intent.ACTION_VIEW); 47 intent.addCategory(Intent.CATEGORY_BROWSABLE); 48 intent.setData(Uri.parse("http://edu.csdn.net")); 49 startActivity(intent); 50 51 } 52 }); 53 builder.setNegativeButton("取消", new OnClickListener() { 54 55 public void onClick(DialogInterface dialog, int which) { 56 // 用户点击取消 会关闭掉对话框 57 } 58 }); 59 AlertDialog dialog = builder.create(); 60 dialog.show(); 61 } 62 63 public void click2(View view) {// 显示单选对话框 64 // 创建一个对话框的构建者 65 AlertDialog.Builder builder = new Builder(this); 66 builder.setIcon(R.drawable.ic_launcher); 67 builder.setTitle("单选对话框"); 68 final String[] items = { "java", ".net", "php" }; 69 builder.setItems(items, new OnClickListener() { 70 71 public void onClick(DialogInterface dialog, int which) { 72 Toast.makeText(getApplicationContext(), "选择了" + items[which], 0) 73 .show(); 74 } 75 }); 76 77 AlertDialog dialog = builder.create(); 78 dialog.show(); 79 } 80 81 public void click3(View view) {// 显示带按钮单选对话框 82 // 创建一个对话框的构建者 83 AlertDialog.Builder builder = new Builder(this); 84 builder.setIcon(R.drawable.ic_launcher); 85 builder.setTitle("单选对话框"); 86 final String[] items = { "java", ".net", "php" }; 87 builder.setSingleChoiceItems(items, 2, new OnClickListener() { 88 89 public void onClick(DialogInterface dialog, int which) { 90 Toast.makeText(getApplicationContext(), "选择了" + items[which], 0) 91 .show(); 92 dialog.dismiss(); 93 } 94 }); 95 96 AlertDialog dialog = builder.create(); 97 dialog.show(); 98 } 99 100 public void click4(View view) {// 显示带按钮多选对话框 101 // 创建一个对话框的构建者 102 AlertDialog.Builder builder = new Builder(this); 103 builder.setIcon(R.drawable.ic_launcher); 104 builder.setTitle("多选对话框"); 105 final String[] items = { "java", ".net", "php" }; 106 107 builder.setMultiChoiceItems(items, 108 new boolean[] { false, true, false }, 109 new OnMultiChoiceClickListener() { 110 111 public void onClick(DialogInterface dialog, int which, 112 boolean isChecked) { 113 if (isChecked) { 114 Toast.makeText(getApplicationContext(), 115 "选择了" + items[which], 0).show(); 116 } else { 117 Toast.makeText(getApplicationContext(), 118 "取消选择了" + items[which], 0).show(); 119 } 120 121 } 122 }); 123 builder.setNegativeButton("取消", new OnClickListener() { 124 125 public void onClick(DialogInterface dialog, int which) { 126 127 } 128 }); 129 AlertDialog dialog = builder.create(); 130 dialog.show(); 131 } 132 133 public void click5(View view) {// 进度条对话框 134 ProgressDialog pd = new ProgressDialog(this); 135 // 默认是一个不显示具体进度的对话框 136 pd.setMessage("正在下载数据...."); 137 pd.show(); 138 } 139 140 public void click6(View view) {// 进度条对话框 141 final ProgressDialog pd = new ProgressDialog(this); 142 // 显示具体进度的对话框 143 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 144 pd.setMax(100); 145 pd.setProgress(20); 146 pd.show(); 147 new Thread() { 148 public void run() { 149 for (int i = 0; i < 100; i++) { 150 pd.setProgress(i); 151 try { 152 Thread.sleep(100); 153 } catch (InterruptedException e) { 154 // TODO Auto-generated catch block 155 e.printStackTrace(); 156 } 157 } 158 }; 159 }.start(); 160 } 161 }
基础/08/dialog