大家好,我们这一节将讲多选项CheckBox
的综合应用,我们的程序主要构造两个CheckBox
的对象,以及一个TextView对象,并通过setOnCheckedChangeLisener
实现onCheckedChanged
()方法来更新TextView
文字.
首先我们看一下效果图:
下面是主程序的代码:
string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, CheckboxDemo!</string>
<string name="app_name">CheckboxDemo</string>
<string name="hobby">你的爱好是:</string>
<string name="basketball">篮球</string>
<string name="football">足球</string>
</resources>
主程序界面代码main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hobby"
/>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/basketball"
/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/football"
/>
</LinearLayout>
最后是程序的核心代码CheckBoxDemo:
package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class CheckboxDemo extends Activity {
private TextView tv;
private CheckBox cb1;
private CheckBox cb2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.textview1);
cb1 = (CheckBox)findViewById(R.id.checkbox1);
cb2 = (CheckBox)findViewById(R.id.checkbox2);
cb1.setOnCheckedChangeListener(cbListener);
cb2.setOnCheckedChangeListener(cbListener);
}
private CheckBox.OnCheckedChangeListener cbListener =
new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
String stv = getString(R.string.hobby);
String scb1 = getString(R.string.basketball);
String scb2 = getString(R.string.football);
//判断一共有四种情况
if(cb1.isChecked()== true && cb2.isChecked()== true)
{
tv.setText(stv + scb1 + "," + scb2);
}
else if(cb1.isChecked()== true && cb2.isChecked()== false)
{
tv.setText(stv+scb1);
}
else if(cb1.isChecked() == false && cb2.isChecked() == true)
{
tv.setText(stv+scb2);
}
else{
tv.setText(stv);
}
}
};
}
这一节就至此为止,由于时间的原因,我就不加注释了,有什么不能理解的地方,请大家留言,我会详细解答.
分享到:
相关推荐
•Android---UI篇---Tab Layout(选项卡布局) • •Andorid---UI篇---TableLayout(表格布局) • •Android---UI篇---RelativeLayout(相对布局) • •Android---UI篇---GridView(网格布局) • •Android---UI篇-...
Android CheckBox and RadioButton with additional 3rd 'indeterminate' state. Requirements Based on AppCompat library Requires AppCompat theme API 7 (but tested only on API 10 ) Getting Started You ...
一个简单的拥有丰富自定义内容的CheckBox多选框集成Gradlecompile 'com.bigmercu.cBox:cbox:1.08'Mavenallprojects { repositories { jcenter() maven { url '...
React复选框树 一个简单优雅的复选框树,用于... node_modules/react-checkbox-tree/src/less/react-checkbox-tree.less node_modules/react-checkbox-tree/src/scss/react-checkbox-tree.scss 在样式表中包含以
019_UI_常用组件-BasicViews-button-checkbox-radio视频教材,讲解的比较详细,有兴趣的可以学习下哦
vue配合element-ui框架中el-checkbox 接收json"data:[{key:1,name:'小李'}]"对象时全选与非全选时获取相关设定的值源码文件
天轰穿系列教程之-33单选按纽和复选框RadioButton CheckBox天轰穿系列教程之-33单选按纽和复选框RadioButton CheckBox天轰穿系列教程之-33单选按纽和复选框RadioButton CheckBox天轰穿系列教程之-33单选按纽和复选框...
最近公司需求做个员工报餐界面用到多个CheckBox 行列
前端项目-awesome-bootstrap-checkbox,字体可怕的引导复选框和收音机。纯CSS方式使输入看起来更漂亮。
CircleCheckBox Screenshot: Attrs attr format ...CircleCheckBox is available in the MavenCentral, so getting it as simple as adding it as a ...checkBox.setListener(new CircleCheckBox.On
mtree1.0-checkbox-tableTree-带复选框树形控件,带复选框 支持表格转换为树形表格,使用简单。希望对各位有用,同时也望各位朋友提供更多更好的改进建议!
android CheckBox的使用简单实例。
Android应用开发
拥有三种状态纯Material风格的Android Checkbox控件
如何自定义CheckBox的样式 1:首先在布局文件中添加CheckBox的控件配置,如: <CheckBox android:id="@+id/button1" style="@style/CheckBoxStyles"//这里就是用户可以自定CheckBox的样式 android:layout_width...
3 State Checkbox for Android in pure Material Style, with animation between the states. Usage In your layout: In Kotlin: // set both the checked and indeterminate state checkbox.setChecked(true, ...
Android 带有切换动画的CheckBox
nested_el-checkbox element-ui的el-checkbox实现嵌套多选,单选 tips:chrome获取本地json数据时会产生跨域问题,建议用firefox直接打开 效果图 主要功能: 实现多选框层级嵌套 当选中一个二级子菜单的时候,其对应...
android checkbox 的isChecked属性
Android checkbox 实现单选,代码较少,布局简单