android dialog集合

代码:

package com.test.dialog;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class DialogTestActivity extends Activity {
    /** Called when the activity is first created. */
	public static final String TAG="DialogTestActivity";
	int i=0;
	boolean b[]={true,false,false,false};
	Button btnAlertDialog;
	Button btnSingleItems;
	Button btnSingelChoices;
	Button btnMuiltiChoice;
	
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnAlertDialog = (Button)findViewById(R.id.alertDialog);
        btnAlertDialog.setOnClickListener(new ButtonViewClickListener());
        
        btnSingleItems = (Button)findViewById(R.id.singleItemsAlertDialog);
        btnSingleItems.setOnClickListener(new ButtonViewClickListener());
        
        btnSingelChoices = (Button)findViewById(R.id.singleChoiceAlertDialog);
        btnSingelChoices.setOnClickListener(new ButtonViewClickListener());
        
        btnMuiltiChoice = (Button)findViewById(R.id.multiItemsAlertDialog);
        btnMuiltiChoice.setOnClickListener(new ButtonViewClickListener());
        
        //单选框的测试
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				RadioButton radioButton = (RadioButton)findViewById(checkedId);
				Log.d(TAG, String.valueOf(radioButton.getText().toString())+","+checkedId);
				
			}
		});
        
        //多选框的测试
       final ArrayList list = new ArrayList();
        list.add((CheckBox)findViewById(R.id.cbAndroid));
        list.add((CheckBox)findViewById(R.id.cbJava));
        list.add((CheckBox)findViewById(R.id.cbPhp));
        list.add((CheckBox)findViewById(R.id.cbWP7));
        
        for(CheckBox box : list){
        	box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					CheckBox box = (CheckBox) buttonView;
					Log.d(TAG, isChecked+","+box.getText().toString());
				}
			});
        }
        Button button = (Button)findViewById(R.id.btnCheckBox);
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				for(CheckBox box : list){
					if(box.isChecked()){
						Log.d(TAG, box.getText().toString());
					}
					
				}
			}
		});
        
        
    }
    
    
    public class ButtonViewClickListener implements View.OnClickListener{
 
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.alertDialog:
				myAlertDialog();
				break;

			case R.id.singleItemsAlertDialog:
				mySingleItemsDialog();
				break;
			case R.id.singleChoiceAlertDialog:
				mySingleChoiceDialog();
				break;
			case R.id.multiItemsAlertDialog:
				myMultiItemsDialog();
				break;
			}
		}
    }
    /**
     * 对话通知框
     */
    public void myAlertDialog(){
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("AlertDialog");
    	builder.setMessage("我是内容!");
    	builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Log.d(TAG, ""+which);
				dialog.cancel();
				
			}});
    	builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Log.d(TAG, ""+which);
				dialog.cancel();
				
			}
		});
    	builder.create();
    	builder.show();
    }
    
    /**
     * 单选列表对话框
     */
    public void mySingleItemsDialog(){
    	 AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("课程列表");
    	builder.setItems(R.array.singleItems, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Log.d(TAG, ""+which);
				dialog.cancel();
			}
		});
    	builder.create().show();
    }
    /**
     * 单选选项对话框
     */
    public void mySingleChoiceDialog(){
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("课程列表");
    	builder.setSingleChoiceItems(R.array.singleItems, i, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				i=which;
				Log.d(TAG, ""+which);
				dialog.cancel();
			}
		});
    	builder.create().show();
    }
    
    /**
     * 多选项列表对话框
     */
    public void myMultiItemsDialog(){
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("课程列表");
    	builder.setMultiChoiceItems(R.array.singleItems, b, new DialogInterface.OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				// TODO Auto-generated method stub
				b[which] = isChecked;
				Log.d(TAG, ""+which+","+isChecked);
				
			}
		});
    	builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				dialog.cancel();
			}
		});
    	builder.create().show();
    }
    
}


xml 文件:




    
    


调试输出的log

你可能感兴趣的:(android dialog集合)