1.关于AlertDialog警示框的使用,主要分为警示框,单选框,复选框。
先看一下警示框
import java.util.ArrayList;
import java.util.TreeSet;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private ArrayList
list= new ArrayList
();
private Button bt1;
private Button bt2;
private Button bt3;
private String[] bs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void jump1(View v){
AlertDialog.Builder aler1=new AlertDialog.Builder(this);
aler1.setIcon(R.drawable.ic_launcher);
aler1.setTitle("只要288,就能把美女带回家");
aler1.setMessage("选好了付钱带走");
//点击返回键不能取消对话框
aler1.setCancelable(false);
aler1.setNegativeButton("没选好", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "没有合适的", 0).show();
}
});
aler1.setPositiveButton("选好了", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "就这个了", 0).show();
}
});
aler1.show();
}
public void jump2(View v){
AlertDialog.Builder alert2=new AlertDialog.Builder(this);
alert2.setIcon(R.drawable.ic_launcher);
alert2.setTitle("请选择运输方式:");
String[] a={"人力运输","汽车运输","火车运输","飞机运输"};
alert2.setSingleChoiceItems(a, 1, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch(which){
case 0:
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
break;
}
}
});
alert2.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "没有合适的", 0).show();
}
});
alert2.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"选好了", 0).show();
}
});
alert2.show();
}
多选框
public void jump3(View v){
AlertDialog.Builder alert3=new AlertDialog.Builder(this);
alert3.setIcon(R.drawable.ic_launcher);
alert3.setTitle("来选美女了");
bs = new String[] {"春香","夏香","秋香","冬香"};
boolean[] d={false,false,false,false};
alert3.setMultiChoiceItems(bs, d, new OnMultiChoiceClickListener() {
{
list.clear();}
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
String name=bs[which];
list.add(name);
}
}
});
alert3.setNegativeButton("没选好,溜走", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "没有合适的", 0).show();
}
});
alert3.setPositiveButton("选好了,就在这几个把", new OnClickListener() {
private String na="";
@Override
public void onClick(DialogInterface dialog, int which) {
for(String a:list){
na+=a+",";
}
Toast.makeText(MainActivity.this, "我选的是"+na, 0).show();
}
});
alert3.show();
}