其实单选和复选框他们的使用方法和和属性基本上都是差不多的
CheckBox
实现多选的控件
常用属性:
android:text 文字
android:checked 是否选中
常用方法:
boolean isChecked() 返回是否被选中
getText() 返回文字
setChecked(boolean checked) 设置是否选中
实现多选的控件
常用属性:
android:text 文字
android:checked 是否选中
常用方法:
boolean isChecked() 返回是否被选中
getText() 返回文字
setChecked(boolean checked) 设置是否选中
枯燥的文字是无法很好的让大家理解,所以我打算用举例子的方式,我就举一个兴趣爱好的例子吧
页面布局,我相信应该是最容易的,难就难在怎么将你选中的内容显示出来
页面布局代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/layout"
tools:context=".MainActivity" >
<CheckBox
android:id="@+id/sport_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动"/>
<CheckBox
android:id="@+id/reading_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看书"/>
<CheckBox
android:id="@+id/game_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏"/>
<Button
android:id="@+id/ok_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"/>
</LinearLayout>
主要实现代码:
package com.example.checkbox;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button mOkBt;
private CheckBox mSportCb;
private CheckBox mReadingCb;
private CheckBox mGameCb;
private LinearLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
mOkBt =(Button)findViewById(R.id.ok_bt);
mSportCb = (CheckBox)findViewById(R.id.sport_cb);
mReadingCb = (CheckBox)findViewById(R.id.reading_cb);
mGameCb = (CheckBox)findViewById(R.id.game_cb);
mLayout = (LinearLayout)findViewById(R.id.layout);
mOkBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// String hobby = "a";//"a" "xxx" 直接修改String会产生新的String
// hobby += "xxx";
// 经常对字符串进行修改时,推荐使用StringBuffer或者StringBuilder
// StringBuffer 线程安全的 (会降低执行速度) ,StringBuilder 非线程安全的
StringBuilder hobby = new StringBuilder();
// if(mSportCb.isChecked()){
// hobby.append(mSportCb.getText()+" ");
// }
// if(mReadingCb.isChecked()){
// hobby.append(mReadingCb.getText()+" ");
// }
// if(mGameCb.isChecked()){
// hobby.append(mGameCb.getText()+" ");
// }
// 遍历所有布局中的CheckedBox控件
// 获得子控件的数量
int count = mLayout.getChildCount();
for(int i = 0;i < count;i++){
// 获得子控件对象
View child = mLayout.getChildAt(i);
// 判断是否是CheckBox
if(child instanceof CheckBox){
// 转为CheckBox对象
CheckBox cb = (CheckBox)child;
if(cb.isChecked()){
hobby.append(cb.getText()+" ");
}
}
}
if(hobby.length() == 0){
Toast.makeText(MainActivity.this, "木有爱好", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "爱好有:"+hobby.toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
结果如下:
细心的友友会发现我这里使用了两种方法,一种是使用StringBuilder定义一个字符串,将选中的爱好存放在这个字符串中然后进行输出,但是这种方法有些死板,如果我checkbox的选项很多很多的话,那么就会有很多很多个判断的语句,那么就有了第二种方法:就是遍历布局中的checkbox控件
,然后再进行遍历的判断,这样的话,就十分灵活了