本文主要介绍了 android中列表选项,单项选项,多项选型对话框的使用,其使用方法的代码如下:

  

  1. package com.demo.lc;  
  2.  
  3.  
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.content.DialogInterface;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.Window;  
  11. import android.view.WindowManager;  
  12. import android.widget.Button;  
  13. import android.widget.Toast;  
  14.  
  15. public class DialogExercise extends Activity implements OnClickListener {  
  16.     private Button btn_item, btn_singleitem, btn_mutiplyitem;  
  17.     private String[] str = new String[5];  
  18.     private AlertDialog mDialog = null;  
  19.     private int mSelectedItem = 0;  
  20.     private boolean[] init = new boolean[] { falsetruefalsefalsefalse };  
  21.     private boolean[] mtemp = new boolean[5];  
  22.  
  23.     @Override 
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  27.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  28.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  29.         super.setContentView(R.layout.main);  
  30.         for (int i = 0; i < 5; i++) {  
  31.             str[i] = "item" + i;  
  32.         }  
  33.         for (int i = 0; i < init.length; i++) {  
  34.             mtemp[i] = init[i];  
  35.  
  36.         }  
  37.         System.out.println(mtemp == init);  
  38.         this.btn_item = (Button) super.findViewById(R.id.btn_item);  
  39.         this.btn_singleitem = (Button) super.findViewById(R.id.btn_singleitem);  
  40.         this.btn_mutiplyitem = (Button) super.findViewById(R.id.btn_mutilitem);  
  41.  
  42.         this.btn_item.setOnClickListener(this);  
  43.         this.btn_singleitem.setOnClickListener(this);  
  44.         this.btn_mutiplyitem.setOnClickListener(this);  
  45.  
  46.     }  
  47.  
  48.     @Override 
  49.     public void onClick(View v) {  
  50.         if (v == this.btn_item) {  
  51.  
  52.             this.mDialog = new AlertDialog.Builder(this)  
  53.                     .setTitle("列表选项")  
  54.                     .setIcon(android.R.drawable.ic_dialog_alert)  
  55.                     .setItems(str, new DialogInterface.OnClickListener() {  
  56.  
  57.                         @Override 
  58.                         public void onClick(DialogInterface dialog, int which) {  
  59.                             DialogExercise.this.mSelectedItem = which;  
  60.                         }  
  61.                     })  
  62.                     .setPositiveButton("确定",  
  63.                             new DialogInterface.OnClickListener() {  
  64.  
  65.                                 @Override 
  66.                                 public void onClick(DialogInterface dialog,  
  67.                                         int which) {  
  68.                                     System.out.println("PositiveButton= " 
  69.                                             + which);  
  70.                                     Toast.makeText(DialogExercise.this,  
  71.                                             "您选择的是" + str[mSelectedItem], 100)  
  72.                                             .show();  
  73.                                 }  
  74.                             })  
  75.                     .setNegativeButton("取消",  
  76.                             new DialogInterface.OnClickListener() {  
  77.  
  78.                                 @Override 
  79.                                 public void onClick(DialogInterface dialog,  
  80.                                         int which) {  
  81.                                     System.out.println("NegativeButton= " 
  82.                                             + which);  
  83.                                     dialog.dismiss();  
  84.                                 }  
  85.                             }).create();  
  86.             this.mDialog.show();  
  87.  
  88.         } else if (v == this.btn_singleitem) {  
  89.             this.mDialog = new AlertDialog.Builder(this)  
  90.                     .setTitle("单项选择")  
  91.                     .setIcon(android.R.drawable.ic_dialog_alert)  
  92.                     .setSingleChoiceItems(str, 2,  
  93.                             new DialogInterface.OnClickListener() {  
  94.  
  95.                                 @Override 
  96.                                 public void onClick(DialogInterface dialog,  
  97.                                         int which) {  
  98.                                     DialogExercise.this.mSelectedItem = which;  
  99.  
  100.                                 }  
  101.                             })  
  102.                     .setPositiveButton("确定",  
  103.                             new DialogInterface.OnClickListener() {  
  104.  
  105.                                 @Override 
  106.                                 public void onClick(DialogInterface dialog,  
  107.                                         int which) {  
  108.                                     System.out.println("PositiveButton= " 
  109.                                             + which);  
  110.                                     Toast.makeText(DialogExercise.this,  
  111.                                             "您选择的是" + str[mSelectedItem], 100)  
  112.                                             .show();  
  113.                                 }  
  114.                             })  
  115.                     .setNegativeButton("取消",  
  116.                             new DialogInterface.OnClickListener() {  
  117.  
  118.                                 @Override 
  119.                                 public void onClick(DialogInterface dialog,  
  120.                                         int which) {  
  121.                                     System.out.println("NegativeButton= " 
  122.                                             + which);  
  123.                                     dialog.dismiss();  
  124.                                 }  
  125.                             }).create();  
  126.             this.mDialog.show();  
  127.  
  128.         } else if (v == this.btn_mutiplyitem) {  
  129.  
  130.             this.mDialog = new AlertDialog.Builder(this)  
  131.                     .setTitle("多项选择")  
  132.                     .setIcon(android.R.drawable.ic_dialog_alert)  
  133.                     .setMultiChoiceItems(str, init,  
  134.                             new DialogInterface.OnMultiChoiceClickListener() {  
  135.  
  136.                                 @Override 
  137.                                 public void onClick(DialogInterface dialog,  
  138.                                         int which, boolean isChecked) {  
  139.  
  140.                                 }  
  141.                             })  
  142.                     .setPositiveButton("确定",  
  143.                             new DialogInterface.OnClickListener() {  
  144.  
  145.                                 @Override 
  146.                                 public void onClick(DialogInterface dialog,  
  147.                                         int which) {  
  148.                                     System.out.println("PositiveButton= " 
  149.                                             + which);  
  150.                                     String temp = " ";  
  151.                                     for (int i = 0; i < init.length; i++) {  
  152.                                         if (init[i]) {  
  153.                                             temp += str[i] + ", ";  
  154.                                         }  
  155.                                     }  
  156.  
  157.                                     Toast.makeText(DialogExercise.this,  
  158.                                             "您选择的是" + temp, 100).show();  
  159.  
  160.                                     for (int i = 0; i < init.length; i++) {  
  161.  
  162.                                         init[i] = mtemp[i];  
  163.                                     }  
  164.                                 }  
  165.                             })  
  166.                     .setNegativeButton("取消",  
  167.                             new DialogInterface.OnClickListener() {  
  168.  
  169.                                 @Override 
  170.                                 public void onClick(DialogInterface dialog,  
  171.                                         int which) {  
  172.                                     System.out.println("NegativeButton= " 
  173.                                             + which);  
  174.                                     dialog.dismiss();  
  175.                                 }  
  176.                             }).create();  
  177.             this.mDialog.show();  
  178.  
  179.         }  
  180.  
  181.     }  

效果如下:

    当选择单项列表框时的截图:

 

 单击确定后的效果如下:

 

  当选择多项列表框时的截图:

 

  点击确定后的效果如下: