Android使用Checkbox实现单选 ( 简单使用 (一))

  • 实现
    • 效果
    • 第一步,修改activity_main.xml布局文件
    • 第二步,修改MainActivity.java

实现

通过android自带的checkbox实现单选框。

效果

Android使用Checkbox实现单选 ( 简单使用 (一))_第1张图片

第一步,修改activity_main.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"    android:orientation="vertical"    tools:context=".MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        //防止该页面子控件过多无法获取焦点        android:descendantFocusability="blocksDescendants"        android:orientation="horizontal">        <CheckBox            android:id="@+id/click_cb_wc"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="100dp"            android:clickable="true"            android:textColor="#fff"            android:textSize="14sp" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginRight="50dp"            android:text="完成"            android:textColor="#282828"            android:textSize="14sp" />        <CheckBox            android:id="@+id/click_cb_wwc"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="right"            android:clickable="true"            android:textColor="#fff"            android:textSize="14sp" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginRight="10dp"            android:gravity="right"            android:text="未完成"            android:textColor="#282828"            android:textSize="14sp" />    </LinearLayout></LinearLayout>

第二步,修改MainActivity.java

package com.example.checkboxtest;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;public class MainActivity extends AppCompatActivity {    CheckBox cb_wc;    CheckBox cb_wwc;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        cb_wc = (CheckBox) findViewById(R.id.click_cb_wc);        cb_wwc = (CheckBox) findViewById(R.id.click_cb_wwc);        //监听选中取消事件        cb_wc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                   if (isChecked){                       cb_wc.setChecked(true);                       cb_wwc.setChecked(false);                   }else {                       cb_wc.setChecked(false);                   }            }        });        cb_wwc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                if (isChecked){                    cb_wwc.setChecked(true);                    cb_wc.setChecked(false);                }else {                    cb_wwc.setChecked(false);                }            }        });    }}

本篇只是基本使用,后续将会实现页面记住选中的状态以及选择后的传值,未选择的传值等。
有问题欢迎批评指正!

更多相关文章

  1. Android 文件操作
  2. android从资源文件中读取文件流显示
  3. adroid 现行布局水平居中位置
  4. Android百分比布局
  5. 完美解决Android Studio在写XML布局的时候没有了控件代码提示的
  6. android 快速移动sdCard的文件夹
  7. Android 子控件高度超出父布局的限制
  8. Android Studio module里面放switch语句报错 R文件找不到
  9. java.lang.Exception: 密钥库文件不存在: ~/.android/debug.keys

随机推荐

  1. view 动态设置高宽
  2. Android(安卓)Build System
  3. 系统签名apk--转
  4. Android(安卓)suspend/resume flow (上篇
  5. 编译Android(安卓)donut
  6. Android中获取颜色的几种方法
  7. Android(安卓)Fragment和ViewPage 实现简
  8. 最新Android(安卓)ADT, SDK, SDK_tool等
  9. android AlertDialog 弹窗自定义布局 点
  10. Android自带的图标集合