Android之AlterDialog介绍

效果:

 

 

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();

    }

}

  

 

转载于:https://www.cnblogs.com/wangjiaghe/p/7131755.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值