六种对话框
demo搭建:定义多个对话框按钮,为其设置点击监听,通过switch监听点击了哪个按钮,调用对应的方法。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_normal;
private Button btn_more;
private Button btn_list;
private Button btn_ra;
private Button btn_cbs;
private Button btn_my;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_normal = findViewById(R.id.btn_normal);
btn_more = findViewById(R.id.btn_more);
btn_list = findViewById(R.id.btn_list);
btn_ra = findViewById(R.id.btn_ra);
btn_cbs = findViewById(R.id.btn_cbs);
btn_my = findViewById(R.id.btn_my);
btn_normal.setOnClickListener(this);
btn_more.setOnClickListener(this);
btn_list.setOnClickListener(this);
btn_ra.setOnClickListener(this);
btn_cbs.setOnClickListener(this);
btn_my.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_normal:
normal();
break;
case R.id.btn_more:
more();
break;
case R.id.btn_list:
list();
break;
case R.id.btn_ra:
ra();
break;
case R.id.btn_cbs:
cbs();
break;
case R.id.btn_my:
my();
break;
}
}
一、第一种:普通对话框
代码:
private void normal() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setTitle("我是标题");
dialog.setMessage("我是内容");
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
}
效果:

二、第二种:带中性按钮的对话框(多按钮对话框)
代码:
private void more() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setTitle("我是标题");
dialog.setMessage("我是内容");
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNeutralButton("中性", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
}
效果:

第三种:列表对话框
代码:
private void list() {
String[] mList = {"1111", "2222", "3333", "4444", "5555", "6666"};
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("列表对话框");
dialog.setItems(mList, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"第几个:"+which,Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
效果:

第四种:单选对话框
代码:
int mySelect;
private void ra() {
String[] mList = {"1111", "2222", "3333", "4444", "5555", "6666"};
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("单选对话框");
dialog.setSingleChoiceItems(mList, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mySelect=which;
}
});
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mySelect != -1) {
Toast.makeText(MainActivity.this,mList[mySelect],Toast.LENGTH_SHORT).show();
}
}
});
dialog.show();
}
效果:

第五种:复选对话框
代码:
List<String> myChoose=new ArrayList<>();
private void cbs() {
String[] mList = {"1111", "2222", "3333", "4444", "5555", "6666"};
boolean[] isChoose = {false, false, false, false, false, false};
myChoose.clear();
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("多选对话框");
dialog.setMultiChoiceItems(mList, isChoose, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
myChoose.add(mList[which]);
}else {
myChoose.remove(mList[which]);
}
}
});
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = myChoose.size();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < size; i++) {
sb.append(myChoose.get(i));
}
Toast.makeText(MainActivity.this,sb.toString(),Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
效果:

第六种:自定义对话框
代码:
1、自定义一个视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#30CBE0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_marginTop="50dp"
android:layout_height="wrap_content"
android:text="我是标题" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:id="@+id/myEdit"
android:text="请输入内容" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myBtn"
android:layout_gravity="right"
android:layout_marginRight="50dp"
android:text="提交" />
</LinearLayout>
2、调用视图:
private void my() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_mydialog, null);
dialog.setView(view);
final AlertDialog myDialog= dialog.show();
EditText ed = view.findViewById(R.id.myEdit);
Button btn = view.findViewById(R.id.myBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, ed.getText().toString(), Toast.LENGTH_SHORT).show();
myDialog.dismiss();
}
});
}
效果:
