Android弹出多选框下拉框的实例

1 安卓弹出对话框是复选框的代码

[java]  view plain  copy
  1. package com.example.b;  
  2.   
  3. import android.os.Bundle;  
  4. import android.preference.MultiSelectListPreference;  
  5. import android.app.Activity;  
  6. import android.view.Menu;  
  7.   
  8. import android.app.AlertDialog;  
  9. import android.app.Dialog;  
  10. import android.app.AlertDialog.Builder;  
  11. import android.content.DialogInterface;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15.   
  16.   
  17. public class MainActivity extends Activity {  
  18.        private EditText editText;  
  19.         private final static int DIALOG=1;  
  20.         boolean[] flags=new boolean[]{false,false,false};//初始复选情况  
  21.         String[] items=null;  
  22.   
  23.         @Override  
  24.         public void onCreate(Bundle savedInstanceState) {  
  25.             super.onCreate(savedInstanceState);  
  26.             setContentView(R.layout.activity_main);  
  27.   
  28.             items=getResources().getStringArray(R.array.hobby);  
  29.             editText=(EditText)findViewById(R.id.msg);  
  30.             Button button = (Button) findViewById(R.id.button);  
  31.             button.setOnClickListener(new View.OnClickListener() {  
  32.                 @SuppressWarnings("deprecation")  
  33.                 public void onClick(View v) {  
  34.                     // 显示对话框  
  35.                     showDialog(1);  
  36.                 }  
  37.             });  
  38.         }  
  39.           
  40.           
  41.         @Override  
  42.         protected Dialog onCreateDialog(int id) {  
  43.               
  44.             Dialog dialog = null;  
  45.             switch (id) {  
  46.             case 1:  
  47.                 Builder builder = new AlertDialog.Builder(this);  
  48.                 builder.setIcon(R.drawable.ic_launcher);  
  49.                 builder.setTitle("title");  
  50.                 builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {  
  51.                       
  52.                     @Override  
  53.                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {  
  54.                         flags[which]=isChecked;  
  55.                         String results = "";  
  56.                         for (int i = 0; i < flags.length; i++) {  
  57.                             if(flags[i])  
  58.                             {  
  59.                                 results=results+items[i];  
  60.                             }  
  61.                         }  
  62.                           
  63.                         editText.setText(results);  
  64.                     }  
  65.                 });  
  66.                 builder.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  67.                       
  68.                     @Override  
  69.                     public void onClick(DialogInterface dialog, int which) {  
  70.                           
  71.                     }  
  72.                 });  
  73.                 dialog = builder.create();  
  74.                 break;  
  75.   
  76.             default:  
  77.                 break;  
  78.             }  
  79.               
  80.             return dialog;  
  81.         }  
  82.           
  83. }  

2 layout

[java]  view plain  copy
  1. "http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     android:orientation="vertical"  
  10.     tools:context=".MainActivity" >  
  11.   
  12.     
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/msg2"  
  16.         android:text="@string/hello_world" />  
  17.   
  18.     ""   
  19.         android:id="@+id/msg"  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content"   
  22.         android:editable="false"  
  23.         android:cursorVisible="false" />  
  24.       
  25.     "显示复选框对话框"   
  26.         android:id="@+id/button"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content" />  
  29.   

3 加入 多选框的选项

[java]  view plain  copy
  1. "1.0" encoding="utf-8"?>  
  2.   
  3.     "hobby">          
  4.      游泳              
  5.      打篮球              
  6.      登山          
  7.     
  8.   

4 效果图

你可能感兴趣的:(Android弹出多选框下拉框的实例)