本经验将分别介绍Android里面的四种AlertDialog分别是:显示带中立,取消,确定的提示框、 获取带列表的对话框按钮、获取带单选列表的对话框按、获取显示带多选项的对话框。此经验介绍获取显示带多选项的对话框

Android studio或者Eclipse

首先我们在界面布局上面布局四个按钮

然后我们在代码中关联这四个按钮

Button button3=(Button)findViewById(R.id.button3);

Button button4=(Button)findViewById(R.id.button4);

 Button button5=(Button)findViewById(R.id.button5);

 Button button6=(Button)findViewById(R.id.button6);

然后分别加入触发事件:

//获取带列表的对话框按钮
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String[] iterms=new String[]{"run","badminton","football","pingpang","gymnastics","tennis","more..."};
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
//builder.setIcon(R.drawable.desert);
builder.setTitle("Choose your favable sport project:");
builder.setItems(iterms,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"You choosed "+iterms[which],Toast.LENGTH_LONG).show();
}
});
builder.create().show();//创建对话框并显示
}
}); //获取带单选列表的对话框按钮
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String[] iterms=new String[]{"standard","silent","meeting","outdoor","offline"};
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
//builder.setIcon(R.drawable.desert);
builder.setTitle("Choose your favable contextual model:");
builder.setSingleChoiceItems(iterms, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You choosed " + iterms[which], Toast.LENGTH_LONG).show();
}
});
builder.create().show();//创建对话框并显示
}
}); //获取显示带多选项的对话框
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { checkedItems=new boolean[]{false,true,false,true,false};
items=new String[]{"Plants vs Zombies","Angry Birds","popodino","Happy Farm","Super Mario"};
//显示带单选列表的对话框
AlertDialog.Builder builder=new AlertDialog.Builder(getApplicationContext());
//builder.setIcon(R.drawable.desert);
builder.setTitle("Choose your favorable Game:");
builder.setMultiChoiceItems(items,checkedItems,new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which]=isChecked;
}
});
//为对话框添加"确定按钮"
builder.setPositiveButton("confirm",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String result="";
for(int i=0;i<checkedItems.length;i++){
if(checkedItems[i]==true){
result+=items[i]+",";
}
}
if(result.equals("")==false){
result=result.substring(0,result.length()-1);
Toast.makeText(MainActivity.this,"You choosed "+result,Toast.LENGTH_LONG).show();
}
}
});
builder.create().show();
}
});

运行结果如下

Android——四种AterDialog-LMLPHP

更多详细的介绍大家可以到这里面看看,图文并茂更清楚:

http://jingyan.baidu.com/article/0320e2c1ccc8031b87507b23.html

05-06 21:55