AlertDialog是安卓的常见的交互方式。
AlertDialog可以简单的分为五类:一般对话框、列表对话框、单选按钮对话框、多选对话框、自定义对话框。
1.首先在布局中把所有的按钮都写好。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alertdialogtest.MainActivity">
<Button
android:text="一般对话框"
android:textSize="20sp"
android:onClick="normalDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="列表对话框"
android:textSize="20sp"
android:onClick="listDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="单选对话框"
android:textSize="20sp"
android:onClick="singleDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="多选对话框"
android:onClick="mulDialog"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="自定义对话框"
android:onClick="customDialog"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
2.1普通对话框
public void normalDialog(View view){
//得到构造器
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage("是否确认退出?");
builder.setIcon(R.mipmap.ic_launcher);
builder.setCancelable(false);
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();;
}
});
builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton("忽略", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
Dialog dialog=builder.create();
dialog.show();
}
如图所示:
首先构造一个AlertDialog.Builder,通过Builder可以设置标题、内容、图标和是否可取消,默认可以设置最多三个按钮,分别制定点击事件。
setPositiveButton():设置积极得到按钮,推荐设置确定之类的。
setNegativeButton():设置消极按钮,推荐设置取消之类的。
setNeutralButton():设置中立按钮,推荐设置忽略之类的。
注意:创建Builder需要传递Context(上下文)类型的参数,该参数只能传递Activity,不能传递其他类型的上下文(包括getApplicationContext()),否则程序直接报错。
2.2列表对话框
public void listDialog(View view){
final String items[]={"C罗","贝尔","本泽马"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setIcon(R.mipmap.ic_launcher);
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
2.3
单选按钮对话框
public void mulDialog (View view){
final String items[]={"篮球","排球","足球"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("爱好");
final boolean selected[]={false,false,true};
builder.setIcon(R.mipmap.ic_launcher);
builder.setMultiChoiceItems(items , selected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, items[which]+isChecked, Toast.LENGTH_SHORT).show();
}});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
for ( int i=0;i<selected.length;i++){
Log.i("mulDialog", "onClick: "+selected[i]);
}
}
});
builder.create().show();
}
2.5 自定义对话框
首先自定义布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:id="@+id/edit_name"
android:ems="10"
android:gravity="center"
android:layout_gravity="center"
android:hint="输入用户名"
android:inputType="textPersonName"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:id="@+id/edit_password"
android:ems="10"
android:gravity="center"
android:layout_gravity="center"
android:hint="输入密码"
android:inputType="textPassword"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_login"
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
java代码:
public void customDialog(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("自定义Dialog");
builder.setIcon(R.mipmap.ic_launcher);
LayoutInflater lay=getLayoutInflater();
View layout= lay.inflate(R.layout.dialog_custom,null);
builder.setView(layout);
EditText name= (EditText) findViewById(R.id.edit_name);
EditText password = (EditText) findViewById(R.id.edit_password);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
}
运行如图所示: