通过本节课可以学习到的内容:
实例代码:
运行效果参见本课程示例App:安卓猴Demos
CheckBox
,复选框,它允许用户选择一个或者多个。
RadioButton
,单选按钮,只能选取一个选项。
一些用到的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>
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主要用于有限选项而且仅允许单选的场景。