1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; public class MainActivity extends ActionBarActivity { protected void onCreate(Bundle savedInstanceState) {。。。} /** * 确认/取消对话框 */ public void click1(View view){ /** 1.对话框的创建器 **/ AlertDialog.Builder builder = new Builder(this); /** 2.标题 **/ builder.setTitle("我是对话框"); /** 3.信息内容 **/ builder.setMessage("对话框显示的内容"); /** 4.确定按钮(文本,事件) **/ builder.setPositiveButton("确定", new OnClickListener() { /** dialog-本提示框,which-点击的按钮 **/ public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "确定被点击了", 0).show(); } }); /** 5.取消按钮(文本,事件) **/ builder.setNegativeButton("取消", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { /** 什么都不写默认实现就是关闭掉对话框 **/ } }); /** 6.禁用系统的返回功能 **/ builder.setCancelable(false); /** 7.显示对话框 **/ //builder.create().show(); builder.show(); // 其中调用create方法 } /** * 单选对话框 */ public void click2(View view){ // 1.对话框的创建器 AlertDialog.Builder builder = new Builder(this); // 2.标题 builder.setTitle("请选择您的性别"); /** 3.单选项 **/ final String[] items = {"男","女","未知"}; /** 4.创建单选菜单(单选项,默认项,事件) **/ builder.setSingleChoiceItems(items, 2, new OnClickListener() { /** 5.dialog-对话框,which-选择项 **/ public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "您的性别:"+items[which], 0).show(); /** 6.选择后关闭对话框 **/ dialog.dismiss(); } }); /** 7.显示对话框 **/ builder.create().show(); } /** * 多选对话框 */ public void click3(View view){ // 1.对话框的创建器 AlertDialog.Builder builder = new Builder(this); // 2.标题 builder.setTitle("请选择你最爱吃的水果"); /** 3.选择项 **/ final String[] items={"苹果","梨","菠萝","香蕉","黄瓜"}; /** 4.是否选择 **/ final boolean[] result =new boolean[]{true,false,true,false,false}; /** 5.多选菜单(选择项,是否选中,事件) **/ builder.setMultiChoiceItems(items, result, new OnMultiChoiceClickListener() { /** 6.dialog-本对话框, which-点击的项目,ischecked-是否选中了 **/ public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getApplicationContext(), items[which]+isChecked, 0).show(); result[which] = isChecked; } }); /** 7.确认按钮(文本,事件),提交多选的数据 **/ builder.setPositiveButton("提交", new OnClickListener() { /** 8.dialog-本对话框,which-本“提交”按钮 **/ public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer(); for(int i=0;i<result.length;i++){ if(result[i]){ sb.append(items[i]+","); } } Toast.makeText(getApplicationContext(), "您选中了,"+sb.toString(), 0).show(); } }); /** 9.显示对话框 **/ builder.show(); } /** * 进度等待对话框 */ public void click4(View view){ /** 1.进度等待对话框 **/ ProgressDialog pd = new ProgressDialog(this); /** 2.进度等待对话框 **/ pd.setTitle("提醒"); /** 3.进度等待对话框 **/ pd.setMessage("正在加载数据...请稍等。"); /** 4.进度等待对话框 **/ pd.show(); } /** * 带进度的进度条对话框 */ public void click5(View view){ /** 1.进度等待对话框 **/ final ProgressDialog pd = new ProgressDialog(this); /** 2.进度等待对话框 **/ pd.setTitle("提醒"); /** 3.显示风格(水平进度条) **/ pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); /** 4.最大值 **/ pd.setMax(100); /** 5.提示文本 **/ pd.setMessage("正在加载数据...请稍等。"); /** 6.显示对话框 **/ pd.show(); /** 7.开启一个线程,更新进度 **/ new Thread(){ public void run() { for(int i = 0;i<100;i++){ try { Thread.sleep(40); } catch (InterruptedException e) { e.printStackTrace(); } /** 更新进度 **/ pd.setProgress(i); } /** 8.更新结束后关闭对话框 **/ pd.dismiss(); }; }.start(); } } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/397.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设