安卓——AlertDialog多样按钮



  1. 在xml 设计页面添加标签
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.app_style.MainActivity"
    android:orientation="vertical">

    <Button
    android:id="@+id/b1"
    android:text="click select!"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <Button
        android:id="@+id/b2"
        android:text="click Dx!"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

    2. //多选按钮
b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final String[] s={"h5","python","Java","c#","Php","Android"};
        boolean []b={false,false,false,false,false,false};
       new  AlertDialog.Builder(MainActivity.this).setIcon(R.mipmap.ic_launcher)
               .setTitle("问问你,你宣啥?")
               .setPositiveButton("Ok",null)
               .setMultiChoiceItems(s, b, new DialogInterface.OnMultiChoiceClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which, boolean isChecked) {
               if(isChecked==true)
               {
                   Toast.makeText(MainActivity.this,"你选了:"+s[which]+ " 这个语言,看来你很喜欢他啊!",Toast.LENGTH_SHORT).show();
               }
               if(isChecked==false)
               {
                   Toast.makeText(MainActivity.this,"你取消了:"+s[which]+ " 这个语言,看来你移情别恋了啊!",Toast.LENGTH_SHORT).show();
               }

           }
       }).show();

    }
});

3 单选按钮
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] s={"h5","python","Java","c#","Php","Android"};

                new  AlertDialog.Builder(MainActivity.this).setIcon(R.mipmap.ic_launcher)
                        .setTitle("问问你,你宣啥?")
                        .setPositiveButton("Ok",null)
                        .setSingleChoiceItems(s,0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,"你选了:"+s[which]+ " 这个语言,看来你很喜欢他啊!",Toast.LENGTH_SHORT).show();
                            }
                        }).show();

            }
        });
  
  1. 在xml 设计页面添加标签
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.app_style.MainActivity"
    android:orientation="vertical">

    <Button
    android:id="@+id/b1"
    android:text="click select!"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <Button
        android:id="@+id/b2"
        android:text="click Dx!"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

    2. //多选按钮
b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final String[] s={"h5","python","Java","c#","Php","Android"};
        boolean []b={false,false,false,false,false,false};
       new  AlertDialog.Builder(MainActivity.this).setIcon(R.mipmap.ic_launcher)
               .setTitle("问问你,你宣啥?")
               .setPositiveButton("Ok",null)
               .setMultiChoiceItems(s, b, new DialogInterface.OnMultiChoiceClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which, boolean isChecked) {
               if(isChecked==true)
               {
                   Toast.makeText(MainActivity.this,"你选了:"+s[which]+ " 这个语言,看来你很喜欢他啊!",Toast.LENGTH_SHORT).show();
               }
               if(isChecked==false)
               {
                   Toast.makeText(MainActivity.this,"你取消了:"+s[which]+ " 这个语言,看来你移情别恋了啊!",Toast.LENGTH_SHORT).show();
               }

           }
       }).show();

    }
});

3 单选按钮
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] s={"h5","python","Java","c#","Php","Android"};

                new  AlertDialog.Builder(MainActivity.this).setIcon(R.mipmap.ic_launcher)
                        .setTitle("问问你,你宣啥?")
                        .setPositiveButton("Ok",null)
                        .setSingleChoiceItems(s,0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,"你选了:"+s[which]+ " 这个语言,看来你很喜欢他啊!",Toast.LENGTH_SHORT).show();
                            }
                        }).show();

            }
        });
  
posted @ 2018-06-29 02:30  baobaoMaster  阅读(504)  评论(0编辑  收藏  举报