Android使用Checkbox实现单选 ( 简单使用 (一))
16lz
2021-01-23
Android使用Checkbox实现单选 ( 简单使用 (一))
- 实现
- 效果
- 第一步,修改activity_main.xml布局文件
- 第二步,修改MainActivity.java
实现
通过android自带的checkbox实现单选框。
效果
第一步,修改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); } } }); }}
本篇只是基本使用,后续将会实现页面记住选中的状态以及选择后的传值,未选择的传值等。
有问题欢迎批评指正!
更多相关文章
- Android 文件操作
- android从资源文件中读取文件流显示
- adroid 现行布局水平居中位置
- Android百分比布局
- 完美解决Android Studio在写XML布局的时候没有了控件代码提示的
- android 快速移动sdCard的文件夹
- Android 子控件高度超出父布局的限制
- Android Studio module里面放switch语句报错 R文件找不到
- java.lang.Exception: 密钥库文件不存在: ~/.android/debug.keys