AlertDialog的写法

 1 public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
 2         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 3         builder.setTitle("选项");
 4         builder.setItems(R.array.choice, new DialogInterface.OnClickListener() {
 5             @Override
 6             public void onClick(DialogInterface dialog, int which) {
 7                 switch(which) {
 8                 case 0:
 9                     break;
10                 case 1:
11                     break;
12                 case 2:
13                     break;
14                 }
15                 
16             }
17         });
18         builder.setNegativeButton("取消", null);
19         builder.create().show();
20         
21     }

 

 

string.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <string name="app_name">AppExplorer</string>
 5     <string name="hello_world">Hello world!</string>
 6     <array name ="choice">
 7         <item name="start">启动程序</item>
 8         <item name="detail">详细信息</item>
 9         <item name="uninstall">卸载</item>
10     </array>
11 </resources>

效果:

 

 1 public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
 2         final PackageInfo tempPkgInfo = showPackageInfos.get(position);
 3         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 4         builder.setTitle("选项");
 5         builder.setItems(R.array.choice, new DialogInterface.OnClickListener() {
 6             @Override
 7             public void onClick(DialogInterface dialog, int which) {
 8                 switch(which) {
 9                 case 0:
10                     String packageName = tempPkgInfo.packageName;
11                     ActivityInfo[] activityInfo = tempPkgInfo.activities;
12                     String activityName = activityInfo[0].name;
13                     try {
14                         Intent i = new Intent();
15                         i.setComponent(new ComponentName(packageName, activityName));
16                         startActivity(i);
17                     } catch (Exception e) {
18                         return;
19                     }
20                     break;
21                 case 1:
22                     showAppDetail(tempPkgInfo);
23                     break;
24                 case 2:
25                     Uri packageUri = Uri.parse("package:" + tempPkgInfo.packageName);
26                     Intent deleteIntent = new Intent();
27                     deleteIntent.setAction(Intent.ACTION_DELETE);
28                     deleteIntent.setData(packageUri);
29                     startActivity(deleteIntent);
30                     break;
31                 }
32                 
33             }
34 
35             
36         });
37         builder.setNegativeButton("取消", null);
38         builder.create().show();
39         
40     }

 passwordDialog = builder.create();此方法返回一个AlertDialog

posted @ 2014-11-26 18:31  疾风剑  阅读(321)  评论(0编辑  收藏  举报