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

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

[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. <LinearLayout  xmlns:android="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.     <TextView  
  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.     <EditText android:text=""   
  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.     <Button android:text="显示复选框对话框"   
  26.         android:id="@+id/button"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content" />  
  29. </LinearLayout>  

3 加入 多选框的选项

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="hobby">          
  4.      <item>游泳</item>              
  5.      <item>打篮球</item>              
  6.      <item>登山</item>          
  7.   </string-array>  
  8. </resources>  

4 效果图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值