AlertDialog(对话框)
一、用法
首先通过AlertDialog.Builder创建出一个AlertDialog的实例
然后可以为这个对话框设置标题、内容、可否取消等属性。
//创建builder
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
//设置图标、标题、内容、取消、确定等按钮
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("我是一个标题");
builder.setMessage("我是内容");
builder.setNegativeButton("NegativeButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"NegativeButton",Toast.LENGTH_SHORT).show();
}
});
builder..setNeutralButton("NeutralButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"NeutralButton",Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("PositiveButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"PositiveButton",Toast.LENGTH_SHORT).show();
}
});
//AlertDialog对象
AlertDialog ad=builder.create();
//显示
ad.show();
二、实例
实例一:普通用法,弹出普通alerdialog,跟上面用法的介绍相同
public void show(){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher).setTitle("我是一个标题").setMessage("我是内容").setNegativeButton("NegativeButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"NegativeButton",Toast.LENGTH_SHORT).show();
}
}).setNeutralButton("NeutralButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"NeutralButton",Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("PositiveButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"PositiveButton",Toast.LENGTH_SHORT).show();
}
});
AlertDialog ad=builder.create();
ad.show();
}
实例二:弹出单选框的Dialog,调用builder.setSingleChoiceItems(msexs, 0, new DialogInterface.OnClickListener() {});(匿名内部类)
加上取消与确定按钮
public void singleselect(){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setSingleChoiceItems(msexs, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sex=msexs[which];
}
});
builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "您的性别是" + sex, Toast.LENGTH_SHORT).show();
mBtn2.setText("您的性别是"+sex);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sex="您没有进行性别选择";
Toast.makeText(getApplicationContext(), sex, Toast.LENGTH_SHORT).show();
}
});
AlertDialog ad=builder.create();
ad.show();
}
实例三:多选框Dialog,调用 builder.setMultiChoiceItems(hobbys, mIscheckedHobby, new DialogInterface.OnMultiChoiceClickListener()});
public void multiselect(){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setMultiChoiceItems(hobbys, mIscheckedHobby, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//which代表选中的是哪个
mIscheckedHobby[which]=isChecked;
}
});
builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i <hobbys.length ; i++) {
if(mIscheckedHobby[i]){
hobby.append(hobbys[i]);
}
}
Toast.makeText(getApplicationContext(),"您的爱好是"+hobby,Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog ad=builder.create();
ad.show();
}
实例四:可点击Dialog,调用builder.setItems(mData,new DialogInterface.OnClickListener() {});
private void enableselect() {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setItems(mData,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//which是从0开始的
which=which+1;
Toast.makeText(getApplicationContext(),"选中了第"+which+"个",Toast.LENGTH_SHORT).show();
}
});
AlertDialog ad=builder.create();
ad.show();
}
完整代码:
布局:四个按钮
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="弹出一个Dialog"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="弹出一个单选Dialog"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="弹出一个多选Dialog"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="弹出一个可选择的Dialog"/>
</LinearLayout>
MainActivity:
public class MainActivity extends Activity implements View.OnClickListener{
private Button mBtn;
private Button mBtn2;
private Button mBtn3;
private Button mBtn4;
private String[] msexs={"男","女","其他"};
private String sex;
private String[] hobbys={"篮球","看书","跑步","羽毛球","画画"};
private boolean[] mIscheckedHobby;
private StringBuffer hobby=new StringBuffer();
private String[] mData={"第一个可点击","第二个可点击","第三个可点击","第四个可点击"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIscheckedHobby=new boolean[hobbys.length];
mBtn= (Button) findViewById(R.id.button);
mBtn2=(Button) findViewById(R.id.button2);
mBtn3=(Button) findViewById(R.id.button3);
mBtn4=(Button) findViewById(R.id.button4);
mBtn.setOnClickListener(this);
mBtn2.setOnClickListener(this);
mBtn3.setOnClickListener(this);
mBtn4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:{
show();
break;
}
case R.id.button2:{
singleselect();
break;
}
case R.id.button3:{
multiselect();
break;
}
case R.id.button4:{
enableselect();
break;
}
default:break;
}
}
private void enableselect() {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setItems(mData,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
which=which+1;
Toast.makeText(getApplicationContext(),"选中了第"+which+"个",Toast.LENGTH_SHORT).show();
}
});
AlertDialog ad=builder.create();
ad.show();
}
public void show(){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher).setTitle("我是一个标题").setMessage("我是内容").setNegativeButton("NegativeButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"NegativeButton",Toast.LENGTH_SHORT).show();
}
}).setNeutralButton("NeutralButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"NeutralButton",Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("PositiveButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"PositiveButton",Toast.LENGTH_SHORT).show();
}
});
AlertDialog ad=builder.create();
ad.show();
}
public void singleselect(){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setSingleChoiceItems(msexs, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sex=msexs[which];
}
});
builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "您的性别是" + sex, Toast.LENGTH_SHORT).show();
mBtn2.setText("您的性别是"+sex);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sex="您没有进行性别选择";
Toast.makeText(getApplicationContext(), sex, Toast.LENGTH_SHORT).show();
}
});
AlertDialog ad=builder.create();
ad.show();
}
public void multiselect(){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setMultiChoiceItems(hobbys, mIscheckedHobby, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
mIscheckedHobby[which]=isChecked;
}
});
builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i <hobbys.length ; i++) {
if(mIscheckedHobby[i]){
hobby.append(hobbys[i]);
}
}
Toast.makeText(getApplicationContext(),"您的爱好是"+hobby,Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog ad=builder.create();
ad.show();
}
}