CheckBox:
java.lang.Object
android.view.View
android.widget.TextView
android.widget.Button
android.widget.CompoundButton
android.widget.CheckBox
CheckBox只有两种状态:选中和未选中
→第一个界面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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="一、注册信息与隐私保护"
android:textColor="#ff070000"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="二、使用规则"
android:textColor="#ff070000"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="三、服务内容"
android:textColor="#ff070000"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="四、知识产权和其他合法权益"
android:textColor="#ff070000"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff070000"
android:button="@drawable/checkboxstyle"→在布局文件中引用即可
android:text="我同意如上条款"
/>
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转爱好多选"/>
</LinearLayout>
@@drawable/checkboxstyle,点击可以选中的,有状态显示,什么Java代码都不用写
checkboxstyle.xml 准备两张图片,扩展颜色值也是可以的吧
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/select"/>
<item android:state_checked="false" android:drawable="@drawable/unselect"/>
</selector>
→界面一Java代码:
package com.ncsyeyy.YeyyCheckBox;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
private Button but;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
but = (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(MyActivity.this,InterestActivity.class);
startActivity(intent);
}
});
}
}
→第二个界面interest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/cbBasketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球"
/>
<CheckBox
android:id="@+id/cbPingPangBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓球"
/>
<CheckBox
android:id="@+id/cbFootBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示:"/>
</LinearLayout>
→界面二Java代码
package com.ncsyeyy.YeyyCheckBox;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
/**
* Created by yeyy on 9/4/2015.
*/
public class InterestActivity extends Activity {
private CheckBox cbBasketball;
private CheckBox cbPingPangBall;
private CheckBox cbFootBall;
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.interest);
findView();
setListener();
}
private void findView(){
cbBasketball = (CheckBox) findViewById(R.id.cbBasketball);
cbPingPangBall = (CheckBox) findViewById(R.id.cbPingPangBall);
cbFootBall = (CheckBox) findViewById(R.id.cbFootBall);
tv = (TextView) findViewById(R.id.tv);
}
private void setListener(){
//设置所有checkBox的状态改变监听器
cbBasketball.setOnCheckedChangeListener(myCheckChangListener);
cbPingPangBall.setOnCheckedChangeListener(myCheckChangListener);
cbFootBall.setOnCheckedChangeListener(myCheckChangListener);
}
CompoundButton.OnCheckedChangeListener myCheckChangListener=new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setText();//设置TextView的呢荣显示CHeckBox的选择结果
}
};
//set是设置的意思,而get是获取的意思
private void setText(){
String str;
tv.setText("");////清空TextView的内容,要想给tv.setText("");赋值,就必须要获取get
//如果cbBasketBall被选中,则加入tv显示
if(cbBasketball.isChecked()){
//如果cbBasketball被选中,则加入tv内容显示
str=tv.getText().toString()+cbBasketball.getText().toString()+",";//如果点击了就获取到值,然后设置到显示控件上
tv.setText(str);
}
if (cbPingPangBall.isChecked()){
str=tv.getText().toString()+cbPingPangBall.getText().toString()+",";
tv.setText(str);
}
if (cbFootBall.isChecked()){
str=tv.getText().toString()+cbFootBall.getText().toString()+",";
tv.setText(str);
}
}
}
总结:
要注意的地方:
<CheckBox
android:id="@+id/cbFootBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球"→可以在选择框后面直接输入文字
str=tv.getText().toString()+cbBasketball.getText().toString()+",";可以显示到TextView控件
关于思路
1.布局文件
2.checkBox 初始化控件
3.设置监听时间,对于按钮
4.得到控件上的选择显示到控件上textview,可以多选,要注意if判断
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
CheckBox.zip (29个子文件)
CheckBox
CheckBoxImage.png 87KB
CheckBoxAllImage.png 12KB
YeyyCheckBox
project.properties 563B
src
com
ncsyeyy
YeyyCheckBox
MyActivity.java 374B
AndroidManifest.xml 719B
ant.properties 698B
.idea
misc.xml 486B
compiler.xml 709B
workspace.xml 24KB
.name 12B
encodings.xml 164B
modules.xml 264B
scopes
scope_settings.xml 143B
copyright
profiles_settings.xml 76B
vcs.xml 164B
res
drawable-ldpi
ic_launcher.png 3KB
drawable-hdpi
ic_launcher.png 9KB
drawable-mdpi
ic_launcher.png 5KB
layout
main.xml 1KB
drawable-xhdpi
ic_launcher.png 14KB
values
strings.xml 120B
assets
gen
com
ncsyeyy
YeyyCheckBox
Manifest.java 194B
BuildConfig.java 266B
R.java 180B
local.properties 409B
libs
build.xml 4KB
YeyyCheckBox.iml 724B
proguard-project.txt 801B
bin
YeyyCheckBox.txt 7KB
共 29 条
- 1
资源评论
海纳百川叶
- 粉丝: 10
- 资源: 41
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功