1 package com.example.mydialog; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.app.AlertDialog; 6 import android.app.AlertDialog.Builder; 7 import android.app.ProgressDialog; 8 import android.content.DialogInterface; 9 import android.content.DialogInterface.OnClickListener; 10 import android.content.DialogInterface.OnMultiChoiceClickListener; 11 import android.view.Menu; 12 import android.view.View; 13 import android.widget.Toast; 14 15 public class MainActivity extends Activity { 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 } 22 23 //通知对话框 24 public void click1(View view){ 25 AlertDialog.Builder builder = new Builder(this); 26 builder.setTitle("对话框标题"); 27 builder.setIcon(R.drawable.ic_launcher); 28 builder.setMessage("是否进行升级?"); 29 builder.setPositiveButton("确定", new OnClickListener() { 30 //监听确定被点击事件 31 @Override 32 public void onClick(DialogInterface dialog, int which) { 33 Toast.makeText(MainActivity.this, "你点击了确定", 0).show(); 34 35 } 36 }); 37 38 builder.setNegativeButton("取消", new OnClickListener() { 39 //监听取消按钮被点击事件 40 @Override 41 public void onClick(DialogInterface dialog, int which) { 42 // TODO Auto-generated method stub 43 44 } 45 }); 46 47 AlertDialog dialog = builder.create();//创建 48 dialog.show();//显示出来 49 } 50 51 //单选对话框 52 public void click2(View view){ 53 AlertDialog.Builder builder = new Builder(this); 54 builder.setTitle("单选对话框标题"); 55 final String[] items = new String[]{"php","java","object-c"}; 56 //设置单选内容;第二个参数为-1说明谁都不选中 57 builder.setSingleChoiceItems(items, -1, new OnClickListener() { 58 59 @Override 60 public void onClick(DialogInterface dialog, int which) { 61 // TODO Auto-generated method stub 62 Toast.makeText(MainActivity.this, items[which] + "被点击了", 0).show(); 63 dialog.dismiss();//dismiss()方法是关闭对话框 64 } 65 }); 66 67 builder.show(); 68 } 69 70 //多选对话框 71 public void click3(View view){ 72 AlertDialog.Builder builder = new Builder(this); 73 builder.setTitle("多选对话框标题"); 74 final String[] items = new String[]{"php","java","object-c"}; 75 builder.setMultiChoiceItems(items, new boolean[]{true,false,true}, new OnMultiChoiceClickListener() { 76 77 @Override 78 public void onClick(DialogInterface dialog, int which, boolean isChecked) { 79 // TODO Auto-generated method stub 80 Toast.makeText(MainActivity.this, items[which] + isChecked, 0).show(); 81 } 82 }); 83 84 builder.show(); 85 } 86 87 //进度条对话框 88 public void click4(View view){ 89 final ProgressDialog pd = new ProgressDialog(this); 90 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度样式 91 //pd.setTitle("提醒"); 92 //pd.setMessage("正在加载中...."); 93 pd.setMax(100); 94 pd.show(); 95 96 new Thread(){ 97 public void run(){ 98 for(int i=0; i<100; i++){ 99 pd.setProgress(i); 100 try { 101 Thread.sleep(100); 102 } catch (InterruptedException e) { 103 // TODO Auto-generated catch block 104 e.printStackTrace(); 105 } 106 } 107 pd.dismiss(); 108 } 109 }.start(); 110 } 111 112 }
时间: 2024-11-01 10:25:05