Android控件CheckBox学习
来源:互联网 发布:ubuntu 16.04 uuidgen 编辑:程序博客网 时间:2024/05/16 12:18
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.设置监听时间,对于按钮
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
0 0
- Android控件CheckBox学习
- android控件08 CheckBox
- Android 控件:CheckBox
- Android控件之checkbox
- Android控件之CheckBox
- Android 控件之checkBox
- Android控件---CheckBox
- Android控件之CheckBox
- Android控件CheckBox、RadioButton
- android控件之CheckBox
- Android控件之CheckBox
- android控件06---CheckBox
- Android学习分享-常见控件05-RadioButton和CheckBox详解
- Android学习-常见的UI控件 ToggleButton和CheckBox
- CheckBox---Android控件系列之CheckBox
- Android控件开发之CheckBox
- android控件之radiobutton checkbox
- android 控件 多项选择(CheckBox)
- Eclipse和PyDev搭建完美Python开发环境(Windows+Mac篇)
- sharepoint 2016 学习系列篇(2)-如何部署sharepoint server 2016(1)-AD域服务器安装和部署
- Python 简单串口收发GUI界面
- android Intent 实现跳转 通过注册表
- ActionContext和ServletActionContext
- Android控件CheckBox学习
- 探索 ConcurrentHashMap 高并发性的实现机制
- BeautifulSoup4 and lxml notes
- **蓝懿** iOS面向对象 封装 继承 多态
- javaSE基础编程——线程同步(模拟火车站售票操作)
- **题目1370:数组中出现次数超过一半的数字
- iOS开发中,应用内直接跳转到Appstore评分
- 软件设计师中的前缀、中缀和后缀表达式
- jQuery 选项卡插件 FengTab by FungLeo