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

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

package com.example.b;

import android.os.Bundle;
import android.preference.MultiSelectListPreference;
import android.app.Activity;
import android.view.Menu;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {
	   private EditText editText;
	    private final static int DIALOG=1;
	    boolean[] flags=new boolean[]{false,false,false};//初始复选情况
	    String[] items=null;

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);

	        items=getResources().getStringArray(R.array.hobby);
	        editText=(EditText)findViewById(R.id.msg);
	        Button button = (Button) findViewById(R.id.button);
	        button.setOnClickListener(new View.OnClickListener() {
	            @SuppressWarnings("deprecation")
				public void onClick(View v) {
	                // 显示对话框
	                showDialog(1);
	            }
	        });
	    }
	    
	    
	    @Override
	    protected Dialog onCreateDialog(int id) {
	    	
	    	Dialog dialog = null;
	    	switch (id) {
			case 1:
				Builder builder = new AlertDialog.Builder(this);
				builder.setIcon(R.drawable.ic_launcher);
				builder.setTitle("title");
				builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which, boolean isChecked) {
						flags[which]=isChecked;
						String results = "";
						for (int i = 0; i < flags.length; i++) {
							if(flags[i])
							{
								results=results+items[i];
							}
						}
						
						editText.setText(results);
					}
				});
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
				dialog = builder.create();
				break;

			default:
				break;
			}
	    	
	    	return dialog;
	    }
	    
}

2 layout



    

    
    
    

3 加入 多选框的选项



            
     游泳            
     打篮球            
     登山        
  


4 效果图


你可能感兴趣的:(Android)