Android之AlterDialog介绍

Posted 段合江

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();

    }

}

  

 

以上是关于Android之AlterDialog介绍的主要内容,如果未能解决你的问题,请参考以下文章

我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情

请问在android中,下面两张图片最上方的那个弹出框是怎么实现的,貌似不是AlterDialog popWindow之类的

Android课程---Android Studio使用小技巧:提取方法代码片段

AlterDialog如何屏蔽power键??

AlterDialog 经常使用的样式

当片段具有选项菜单时,Android Chrome Cast 介绍 Overlay 的行为不正确