zoukankan      html  css  js  c++  java
  • RadioButton使用

    RadioButton和CheckBox差不多,这里只写一个,因为我本身不是学andorid,所以就当给自己留一个备份,省的每次用到都需要代码敲一次,很麻烦

    1.如果想选中时想改变颜色可以设置一个xml,我设置的upload_state_btn
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:state_checked="true" android:color="#0000ff"></item>
            <item android:color="#2a2a2a"></item>
    </selector>
    

     使用

    <RadioGroup android:layout_width="match_parent"
                    android:layout_height="44dp"
                    android:orientation="horizontal"
            android:id="@+id/group">
            <RadioButton android:layout_width="0dp"
                         android:layout_height="match_parent"
                         android:layout_weight="1"
                         android:text="男"
                         android:button="@null"
                          android:gravity="center"
                         android:checked="true"
                         android:enabled="false"
                         android:textColor="@drawable/upload_state_btn"
                         android:id="@+id/btn1"
                />
            <RadioButton android:layout_width="0dp"
                         android:layout_height="match_parent"
                         android:layout_weight="1"
                         android:text="女"
                         android:button="@null"
                         android:gravity="center"
                         android:textColor="@drawable/upload_state_btn"
                         android:id="@+id/btn2"/>
        </RadioGroup>
    

     点击事件有两种方法

    方法一:

      RadioGroup group = (RadioGroup)findViewById(R.id.group);
            group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId){
                        case R.id.btn1:
                            break;
                        case R.id.btn2:
                            break;
                    }
                }
            });
    

     方法二:

       RadioButton btn1;
            RadioButton btn2;
            btn1 = (RadioButton)findViewById(R.id.btn1);
            btn2 = (RadioButton)findViewById(R.id.btn2);
    
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent startIntent = new Intent(DemoActivity.this, MyService.class);
                    //startService启动形式
                    startService(startIntent);
    
                }
            });
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent startIntent = new Intent(DemoActivity.this, MyService.class);
                    //startService启动形式
                    stopService(startIntent);
                }
            });
    

    如果需要改变系统自带的图标,可以添加xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
                android:drawable="@mipmap/checkbox_role"
                 android:state_checked="true"/>
        <item
                 android:drawable="@mipmap/checkbox_dis_role"
                 android:state_checked="false"/>
        <item
               android:drawable="@mipmap/checkbox_dis_role"/>
    </selector>
    

     在styles中添加:

     <style name="CustomTheme" parent="@android:style/Widget.CompoundButton.RadioButton">
            <item name="android:button">@drawable/custom_radio</item>
        </style>
    

     使用的使用直接在xml RadioButton属性中添加一句:

     style="@style/CustomTheme"
    
  • 相关阅读:
    [转] c#有关winform的两个代码片段(多线程操作窗体控件与窗体淡入淡出效果)
    微软企业库源码解析——DAAB(三)DatabaseFactory(其余的Strategy)
    软件工程真的那么难么
    在VS2010中的注册微软企业库4.1
    对为什么使用访问器(getter),以及什么是继承的一点看法
    微软企业库源码解析——DAAB(四)DatabaseFactory小结
    Unity与ASP.NET的集成(官方方法)
    ASPxGridView导出pdf时中文乱码的解决方案
    删除WSS卸载后遗留的数据库
    让微软企业库中的Email Trace Listener使用需要身份验证的SMTP服务器
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/10167394.html
Copyright © 2011-2022 走看看