安卓——AlertDialog多样按钮

10 篇文章 0 订阅
10 篇文章 0 订阅


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

            }
        });
 
 
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值