android > 弹出复选框

T1Activity.java

 

 

package t1.com;


import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class T1Activity extends Activity {
	private final static int DIALOG=1;
	boolean[] flags=new boolean[]{false,false,false};//初始复选情况
	
	private String[] array =  new String[3];
	private String result;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		  
        array[0] = "星期一";
        array[1] = "星期二";
        array[2] = "星期三";
        
        showDialog(DIALOG);
        
    } 
    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog=null;
        switch (id) {
        case DIALOG:
            Builder builder=new android.app.AlertDialog.Builder(this);
            //设置对话框的图标
            builder.setIcon(android.R.drawable.ic_dialog_info);;
            //设置对话框的标题
            builder.setTitle("复选框对话框");
            builder.setMultiChoiceItems(array, flags, new DialogInterface.OnMultiChoiceClickListener(){
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    flags[which]=isChecked;
                    String cr = "";
                    for (int i = 0; i < flags.length; i++) {
                        if(flags[i]){
                        	cr=cr+array[i]+"、";
                        }
                    }
                    result = cr;
                }
            });
            
            //添加一个确定按钮
            builder.setPositiveButton(" 确 定 ", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which) {
                	Toast.makeText(T1Activity.this, result, Toast.LENGTH_LONG).show();
                	dialog.dismiss();  
                }
            });
            builder.setNegativeButton("取消", null);    
            //创建一个复选框对话框
            dialog=builder.create();
            break;
        }
        return dialog;
    }
    
    
}
 

你可能感兴趣的:(android)