Android控件CheckBox学习
2015-09-04 18:41
429 查看
Android控件CheckBox学习
java.lang.Object
android.view.View
android.widget.TextView
android.widget.Button
android.widget.CompoundButton
android.widget.CheckBox
CheckBox只有两种状态:选中和未选中
→第一个界面xml布局文件
@@drawable/checkboxstyle,点击可以选中的,有状态显示,什么Java代码都不用写
checkboxstyle.xml 准备两张图片,扩展颜色值也是可以的吧
→界面一Java代码:
→第二个界面interest.xml
→界面二Java代码
总结:
要注意的地方:
<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判断
源码地址:http://download.csdn.net/detail/csdnyuandaimaxuexi/9084625
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判断
源码地址:http://download.csdn.net/detail/csdnyuandaimaxuexi/9084625
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories