AlertDialog练习
2012-05-24 11:29:05 来源:WEB开发网核心提示:package com.unet;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.
package com.unet; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ListView; public class ChoiceDialog extends Activity{ ButtonOnClick buttonOnClick=new ButtonOnClick(1);//单选,多选下拉框的按钮监听 ListView lv=null; private String [] provines=new String[]{"辽宁省","陕西","日本","美国","美女"}; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.controls); Button listBtn=(Button)this.findViewById(R.id.ListChoiceBtn); Button singBtn=(Button)this.findViewById(R.id.SingleChoiceBtn); Button multiBtn=(Button)this.findViewById(R.id.MultiChoiceBtn); BtnListener btnListener=new BtnListener(); listBtn.setOnClickListener(btnListener); singBtn.setOnClickListener(btnListener); multiBtn.setOnClickListener(btnListener); } class BtnListener implements Button.OnClickListener{ @Override public void onClick(View v) { switch(v.getId()){ case R.id.ListChoiceBtn :{ showListDialog(); break; } case R.id.SingleChoiceBtn :{ showSingleChoiceDialog(); break; } case R.id.MultiChoiceBtn :{ showMultiChoiceDialog(); break; } } } } private void showListDialog(){ //Builder的方法setTitle(Stirng str)设置下拉列表框的头 //Builder的方法setItems(内容数组,内容监听)设置下拉列表框的内容 简单列表框 new AlertDialog.Builder(ChoiceDialog.this).setTitle("选择省份") .setItems(provines, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { final AlertDialog ad=new AlertDialog.Builder(ChoiceDialog.this) .setMessage("你选择了"+which+provines[which]).show(); } }).show(); } private void showSingleChoiceDialog(){ new AlertDialog.Builder(ChoiceDialog.this).setTitle("选择省份") //Builder的方法setSingleChoiceItems(内容数组,默认项,监听)是设置下拉单选框返回值为Builder .setSingleChoiceItems(provines, 1, buttonOnClick).setPositiveButton("确定", buttonOnClick).setNegativeButton("取消", buttonOnClick).show(); } private void showMultiChoiceDialog(){ AlertDialog ad=new AlertDialog.Builder(ChoiceDialog.this).setTitle("选择省份") .setMultiChoiceItems(provines, new boolean[]{false,true,false,true,false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) {} }).setPositiveButton("确定", new DialogInterface.OnClickListener() { // Builder的方法setpositiveButton()是专门设置确定按钮的 @Override public void onClick(DialogInterface dialog, int which) { int count=lv.getCount(); String s="你选择了:"; for(int i=0;i<provines.length;i++){ //ListView的方法getCheckedItemPositions()得到的是(true,多选框中选中的编号)键值对, if(lv.getCheckedItemPositions().get(i)){ //判断编号为i的多选框是否被选中 s+=i+":"+lv.getAdapter().getItem(i)+" "; //通过适配器得到选项 } } if(lv.getCheckedItemPositions().size()>0){ // Builder的方法setMessage(String str)的返回值是AlertDialog new AlertDialog.Builder(ChoiceDialog.this).setMessage(s).show(); } else{ new AlertDialog.Builder(ChoiceDialog.this).setMessage("你TM没选!").show(); } } //Builder的方法setNegativeButton()是专门设置取消按钮的 }).setNegativeButton("取消", null).create(); //Builder的方法create()可以创建一个AlertDialog lv = ad.getListView();//AlertDialog的方法getListView()可以得到一个ListView对象 ad.show(); } private class ButtonOnClick implements DialogInterface.OnClickListener{ private int index; public ButtonOnClick(int index){ this.index=index; } public void onClick(DialogInterface dialog, int which) { if(which>=0){ index=which; }else{ if(which==DialogInterface.BUTTON_POSITIVE){ new AlertDialog.Builder(ChoiceDialog.this) .setMessage("您已经选择了:"+index+":"+provines[index]).show(); }else if(which==DialogInterface.BUTTON_NEGATIVE){ new AlertDialog.Builder(ChoiceDialog.this) .setMessage("你TM什么也没选!").show(); } } } } }
Tags:AlertDialog 练习
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接