首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《Monkey Android》第13课CheckBox和RadioButton

《Monkey Android》第13课CheckBox和RadioButton

作者头像
GitOPEN
发布2019-01-29 10:59:42
4920
发布2019-01-29 10:59:42
举报

通过本节课可以学习到的内容:

  • CheckBox的用法
  • RadioButton的用法

实例代码:

运行效果参见本课程示例App:安卓猴Demos

github地址:https://github.com/opengit/MonkeyAndroid


效果图

CheckBox和RadioButton
CheckBox和RadioButton

CheckBox和RadioButton的用法

CheckBox,复选框,它允许用户选择一个或者多个。 RadioButton,单选按钮,只能选取一个选项。

xml文件源码:

一些用到的xml属性的含义已经在注释中给出。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    >


  <CheckBox
      android:id="@+id/checkbox_xiaoshuo"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="小说"
      />

  <CheckBox
      android:id="@+id/checkbox_youxi"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="游戏"
      />

  <CheckBox
      android:id="@+id/checkbox_dianying"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="电影"
      />

  <!--
      这里是一个1dp高度view,用做一个分割线
  -->

  <View
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="@android:color/darker_gray"
      />


  <!--
      android:checkedButton="@+id/radiobutton_apple"
          表示默认选中的RadioButton;

      android:orientation="horizontal"
          表示RadioGroup中的RadioButton的是竖直排列还是水平排列
  -->


  <RadioGroup
      android:id="@+id/radio_group"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checkedButton="@+id/radiobutton_apple"
      android:orientation="horizontal"
      >


    <RadioButton
        android:id="@+id/radiobutton_banana"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="香蕉"
        />

    <RadioButton
        android:id="@+id/radiobutton_apple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="苹果"
        />

    <RadioButton
        android:id="@+id/radiobutton_orange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="橘子"
        />

  </RadioGroup>

</LinearLayout>

Activity中的源码:

package com.sunjiajia.monkeyandroid;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * Created by monkey on 1/1/16.
 *
 * 实现了CheckButton的接口CompoundButton.OnCheckedChangeListener
 *
 * 实现了RadioGroup的接口RadioGroup.OnCheckedChangeListener
 */
public class CheckBoxRadioButtonActivity extends BaseActivity
    implements CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener {
  @Override public int giveViewResId() {
    return R.layout.activity_checkbox_radiobutton;
  }

  private CheckBox mCbXs, mCbYx, mCbDy;
  private RadioGroup mRg;
  private RadioButton mRbBanana, mRbApple, mRbOrange;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initViews();

    configViews();
  }

  /**
   * 配置各种控件,在这里只是设置了绑定了监听事件
   */
  private void configViews() {
    mCbXs.setOnCheckedChangeListener(this);
    mCbYx.setOnCheckedChangeListener(this);
    mCbDy.setOnCheckedChangeListener(this);

    mRg.setOnCheckedChangeListener(this);
  }

  /**
   * init各种控件
   */
  private void initViews() {
    mCbXs = (CheckBox) findViewById(R.id.checkbox_xiaoshuo);
    mCbYx = (CheckBox) findViewById(R.id.checkbox_youxi);
    mCbDy = (CheckBox) findViewById(R.id.checkbox_dianying);

    mRg = (RadioGroup) findViewById(R.id.radio_group);

    mRbBanana = (RadioButton) findViewById(R.id.radiobutton_banana);
    mRbApple = (RadioButton) findViewById(R.id.radiobutton_apple);
    mRbOrange = (RadioButton) findViewById(R.id.radiobutton_orange);
  }

  /**
   * CheckBox的监听事件
   */
  @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    switch (buttonView.getId()) {
      case R.id.checkbox_xiaoshuo:
        checkedShowToast(buttonView, isChecked);
        break;
      case R.id.checkbox_youxi:
        checkedShowToast(buttonView, isChecked);
        break;
      case R.id.checkbox_dianying:
        checkedShowToast(buttonView, isChecked);
        break;
    }
  }

  /**
   * RadioGroup的监听事件
   */
  @Override public void onCheckedChanged(RadioGroup group, int checkedId) {

    switch (checkedId) {
      case R.id.radiobutton_banana:
        radioGroupChecked(mRbBanana);
        break;
      case R.id.radiobutton_apple:
        radioGroupChecked(mRbApple);
        break;
      case R.id.radiobutton_orange:
        radioGroupChecked(mRbOrange);
        break;
    }
  }

  private void radioGroupChecked(RadioButton button) {
    Toast.makeText(CheckBoxRadioButtonActivity.this, "您选中了#" + button.getText().toString() + "#",
        Toast.LENGTH_SHORT).show();
  }

  private void checkedShowToast(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
      Toast.makeText(CheckBoxRadioButtonActivity.this,
          "您选中了#" + buttonView.getText().toString() + "#", Toast.LENGTH_SHORT).show();
    } else {
      Toast.makeText(CheckBoxRadioButtonActivity.this,
          "您取消了#" + buttonView.getText().toString() + "#", Toast.LENGTH_SHORT).show();
    }
  }
}

下课

这一节课,我们主要学习了CheckBox和RadioButton的用法,CheckBox主要用于多选或者全选的场景,RadioButton主要用于有限选项而且仅允许单选的场景。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016/04/11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 效果图
  • CheckBox和RadioButton的用法
  • xml文件源码:
  • Activity中的源码:
  • 下课
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档