效果:
1.简单对话框
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
builder
.setTitle("对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.setMessage("简单对话框")
.create()
.show();
2.列表对话框
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] mItems={"item0","item1","item2","item3","item4","item5","item6"};
builder.setTitle("列表对话框")
.setItems(mItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlterDialogActivity.this,mItems[i],Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show();
3.单选对话框
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] items={"item1","item2","item3","item4"};
builder.setTitle("单选对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlterDialogActivity.this,items[i]+"被点击了",Toast.LENGTH_SHORT).show();
singleChoise=i;
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show();
4.多选对话框
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] items={"item1","item2","item3","item4"};
builder.setTitle("多选对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setMultiChoiceItems(items,new boolean[]{false,false,false,false},new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
if(b){
Toast.makeText(AlterDialogActivity.this,items[i]+"被选中了",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(AlterDialogActivity.this,items[i]+"被取消选中",Toast.LENGTH_SHORT).show();
}
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show();
5.自定义View对话框
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
View view= LayoutInflater.from(this).inflate(R.layout.alterdialot_img,null);
builder
// .setTitle("自定义对话框")
// .setIcon(R.mipmap.ic_launcher_round)
.setView(view)
.setCancelable(true)
.setNegativeButton("取消",null)
.create()
.show();
代码:
xml:
<?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">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="对话框" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="列表对话框" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="单选对话框" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="多选对话框" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="自定义对话框" />
</LinearLayout>
activity
public class AlterDialogActivity extends AppCompatActivity {
// private AlertDialog.Builder builder;
private int singleChoise=0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.act_alterlog);
// builder=new AlertDialog.Builder(this);
}
public void click(View v){
switch (v.getId()){
case R.id.button://简单对话框
showDialog1();
break;
case R.id.button2://列表框
showDialog2();
break;
case R.id.button3://单选框
showDialog3();
break;
case R.id.button4://多选框
showDialog4();
break;
case R.id.button5://自定义contentview
showDialog5();
break;
}
}
private void showDialog1() {
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
builder
.setTitle("对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.setMessage("简单对话框")
.create()
.show();
}
private void showDialog2() {
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] mItems={"item0","item1","item2","item3","item4","item5","item6"};
builder.setTitle("列表对话框")
.setItems(mItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlterDialogActivity.this,mItems[i],Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show();
}
private void showDialog3() {
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] items={"item1","item2","item3","item4"};
builder.setTitle("单选对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlterDialogActivity.this,items[i]+"被点击了",Toast.LENGTH_SHORT).show();
singleChoise=i;
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show();
}
private void showDialog4() {
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] items={"item1","item2","item3","item4"};
builder.setTitle("多选对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setMultiChoiceItems(items,new boolean[]{false,false,false,false},new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
if(b){
Toast.makeText(AlterDialogActivity.this,items[i]+"被选中了",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(AlterDialogActivity.this,items[i]+"被取消选中",Toast.LENGTH_SHORT).show();
}
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show();
}
private void showDialog5() {
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
View view= LayoutInflater.from(this).inflate(R.layout.alterdialot_img,null);
builder
// .setTitle("自定义对话框")
// .setIcon(R.mipmap.ic_launcher_round)
.setView(view)
.setCancelable(true)
.setNegativeButton("取消",null)
.create()
.show();
}
}