zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【2-26】string/math/datetime类的定义及其应用
    聚合函数,数学函数,字符串函数,时间日期函数
    数据库备份,还原,分离与附加
    SQL数据库增删改查
    form表单验证和事件、正则表达式
    悬浮动态分层导航
    图片轮播
    marquee标签
    Window.document对象
    Window.document对象
  • 原文地址:https://www.cnblogs.com/hixin/p/4124040.html
Copyright © 2011-2022 走看看