zoukankan      html  css  js  c++  java
  • 安卓——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();
    
                }
            });
      
  • 相关阅读:
    获取农历日期
    图片上传代码(C#)
    ASP.net使用技术总结(1)GridView控件的单击处理
    JavaScript使用小技巧:IE8的关闭处理
    FrameSet左右收缩编码
    哈哈,开心!今天注册开通了 弟弟Kernel 的网志
    设计模式简介
    Delphi字符串、PChar与字符数组之间的转换
    C++中数组参数详解
    1、简单工厂模式
  • 原文地址:https://www.cnblogs.com/embaobao/p/10720863.html
Copyright © 2011-2022 走看看