android CheckBox控件的简单使用方法

 研究了一下CheckBox选择按钮,下面就以喜欢颜色为素材,做个记录,使用TableRow实现的;

 

 

1.布局activity_layout.xml:

​
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
     <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="喜欢的颜色:"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="clip_horizontal">
            <CheckBox
                android:id="@+id/box1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="红色"
                android:textColor="@color/chocolate"
                android:checked="true"/>
            <CheckBox
                android:id="@+id/box2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="蓝色" />
            <CheckBox
                android:id="@+id/box3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="绿色" />
        </LinearLayout>
    </TableRow>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:id="@+id/show"/>
</TableLayout>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
     <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="喜欢的颜色:"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="clip_horizontal">
            <CheckBox
                android:id="@+id/box1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="红色"
                android:textColor="@color/chocolate"
                android:checked="true"/>
            <CheckBox
                android:id="@+id/box2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="蓝色" />
            <CheckBox
                android:id="@+id/box3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="绿色" />
        </LinearLayout>
    </TableRow>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:id="@+id/show"/>
</TableLayout>

​

colors.xml


<color name="chocolate">#D2691E</color><!--巧可力色 →
<color name="black">#000000</color><!--黑色 -->
<color name="chocolate">#D2691E</color><!--巧可力色 →
<color name="black">#000000</color><!--黑色 -->

 

下面我们来看Activity的代码实现;

public class MainActivity extends Activity{
    private TextView show;
    private CheckBox box1,box2,box3;
    private String love= "我喜欢色是";
    private String not= "不是我喜欢的";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
        show = (TextView) findViewById(R.id.show);
        box1 = (CheckBox) findViewById(R.id.box1);
        box2 = (CheckBox) findViewById(R.id.box2);
        box3 = (CheckBox) findViewById(R.id.box3);
        box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    if(show.getVisibility()==View.VISIBLE){
                        show.setText(love+box1.getText());
                        textColor();
                    }else {
                        show.setVisibility(View.VISIBLE);
                        show.setText(love+box1.getText());
                        textColor();
                    }
                }else{
                    show.setText(not);
                    setTextBlack();
                }
            }
        });
        
        //box2 and box3 和box1的写法一样,在这就不重复写了;
    
    
    private void textColor() {
        box3.setTextColor(getApplicationContext().getColor(R.color.chocolate));
    }
    private void setTextBlack() {
        box3.setTextColor(getApplicationContext().getColor(R.color.black));
    }
}
}

大功告成,看一下效果图:

 

 

比较简单,没事了随便写写,希望有帮助?

 

谢谢!

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值