Activity多功能按钮——基础编

 

 

package com.smart;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
//继承OnClickListener
public class Main extends Activity implements OnClickListener {
//固定的静态变量,
	private final int DELETE_FILE = 1;
	private final int SIMPLE_LIST = 2;
	private final int SINGLE_CHOICE_LIST = 3;
	private final int MULTI_LIST = 4;
	private ListView lv = null;
	//定义一个字符串数组
	private String[] provinces = new String[] { "中国", "泰国", "美国", "法国",
			"新加坡", "英国" };
	//按钮
	private ButtonOnClick buttonOnClick = new ButtonOnClick(1);
	@Override//事件点击
	public void onClick(View v) {

		//开关选择
		switch(v.getId()){
		case R.id.btnDeleteFile:
			showDialog(DELETE_FILE);
			break;
		case R.id.btnSimpleList:
			showDialog(SIMPLE_LIST);
			break;
		case R.id.btnChoiceList:
			showDialog(SINGLE_CHOICE_LIST);
			break;
		case R.id.btnMultiChoiceList:
			showDialog(MULTI_LIST);
			break;
		case R.id.btnRemoveDialog:
			removeDialog(DELETE_FILE);
			removeDialog(SIMPLE_LIST);
			removeDialog(SINGLE_CHOICE_LIST);
			removeDialog(MULTI_LIST);
			break;
			
		}
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		Log.d("dialog",String.valueOf(id));
		//用来判断选择的目标。如果没有选择,并提示出来,选择中了,也显示结果。详看代码
		switch(id){
		case DELETE_FILE:
			return new AlertDialog.Builder(this).setIcon(R.drawable.question).setTitle("是否删除文件").setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					new AlertDialog.Builder(Main.this).setMessage("文件已经被删除").create().show();
				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					new AlertDialog.Builder(Main.this).setMessage("您已经选择了取消,文件未删除").create().show();
				}
			}).create();
			
			
		case SIMPLE_LIST:
		return new AlertDialog.Builder(this).setTitle("选择国家").setItems(provinces, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				final AlertDialog ad=new AlertDialog.Builder(Main.this).setMessage("您已经选择了"+which+":"+provinces[which]).show();
			
			android.os.Handler hander=new android.os.Handler();
			hander.postDelayed(new Runnable() {
				//线程管理
				@Override
				public void run() {
					ad.dismiss();
				}
			},  5*1000);
			}
		}).create();
		case SINGLE_CHOICE_LIST:
			return new AlertDialog.Builder(this).setTitle("选择国家").setSingleChoiceItems(provinces, 1, buttonOnClick).setPositiveButton("确定", buttonOnClick).setNegativeButton("取消", buttonOnClick).create();
		
		case	MULTI_LIST:
			AlertDialog ad=new AlertDialog.Builder(this).setIcon(R.drawable.image).setTitle("选择国家").setMultiChoiceItems(provinces, new boolean[]{false,true,false,true,false,false} , new DialogInterface.OnMultiChoiceClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					
				}
			}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					int count=lv.getCount();
					String s="您选择了:";
					for (int i = 0; i < provinces.length; i++) {
						if(lv.getCheckedItemPositions().get(i)){
							s+=i+":"+lv.getAdapter().getItem(i)+"  ";
						}
					}
					if(lv.getCheckedItemPositions().size()>0){
						new AlertDialog.Builder(Main.this).setMessage(s).show();
					}else{
						new AlertDialog.Builder(Main.this).setMessage("您未选择任何国家").show();
					}
				}
			}).setNegativeButton("取消", null).create();
			lv=ad.getListView();
			return ad;
		}
		
			
		
		
		
		
		return null;
	}

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//得到选中的哪一个按钮
		Button btnDeletefile=(Button)findViewById(R.id.btnDeleteFile);
		Button btnSimpleList=(Button)findViewById(R.id.btnChoiceList);
		Button btnSimngleChoiceList=(Button)findViewById(R.id.btnSimpleList);
		Button btnMultiChoiceList=(Button)findViewById(R.id.btnMultiChoiceList);
		Button btnRemoveDialog=(Button)findViewById(R.id.btnRemoveDialog);

		//绑定监听器
		btnDeletefile.setOnClickListener(this);
		btnSimpleList.setOnClickListener(this);
		btnSimngleChoiceList.setOnClickListener(this);
		btnMultiChoiceList.setOnClickListener(this);
		btnRemoveDialog.setOnClickListener(this);
		
	}
	//定义一个类,用来判断选择中的。
	private class ButtonOnClick implements DialogInterface.OnClickListener {
		private int index;

		public ButtonOnClick(int index) {
			this.index = index;
		}

		@Override
		public void onClick(DialogInterface dialog, int whichButton) {
				if(whichButton>=0){
					index=whichButton;
				}else{
					if(whichButton==DialogInterface.BUTTON_POSITIVE){
						new AlertDialog.Builder(Main.this).setMessage("您已经选择了:"+index+":"+provinces[index]).show();
						
					}else if(whichButton==DialogInterface.BUTTON_NEGATIVE){
						new AlertDialog.Builder(Main.this).setMessage("你未选择,请选择").show();
						
					}
				}
			
			
		}

	}
protected void onPrepareDialog(int id,Dialog dialog){
	super.onPrepareDialog(id, dialog);
}






	
}

另外很多朋友对Android系统构架图还不是很了解,我附上一张中文的图给大家观看

posted on 2011-02-08 14:16  llb988  阅读(480)  评论(0编辑  收藏  举报

导航