1.RadioButton、CheckBox
RadioButton、CheckBox与普通按钮不同的是:他们多了一个可选中的功能,因此他们都可额外指定一个android:checked属性,该属性用于指定RadioButton、CheckBox初始时是否被选中。
RadioButton与CheckBox的不同之处在于,一组RadioButton只能选中其中一个,因此RadioButton通常要与RadioGroup一起使用,用于定义一组单选数组
下面简单介绍一下这两个组件
1)RadioButton
在radio_layout.xml中代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Radio Demo" />
<RadioGroup
android:id="@+id/sexRg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/female">
<RadioButton
android:id="@id/female"
android:text="女"
/>
<RadioButton
android:id="@+id/male"
android:text="男"
/>
</RadioGroup>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/appendRadio"
android:text="addRadioButton"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
在UITest3Activity.java中代码如下:
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class UITest3Activity extends Activity implements OnCheckedChangeListener{
RadioGroup rg = null;
Button addBtn = null;
private static final String TAG = "TAG";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_layout);
findViews();
//指定某个选项被选中
rg.check(R.id.male);
//获取当前选项组中被选中的选项的id
int checkedId = rg.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) this.findViewById(checkedId);
Log.i(TAG, rb.getText().toString());
}
private void findViews() {
rg = (RadioGroup) this.findViewById(R.id.sexRg);
//注册监听器
rg.setOnCheckedChangeListener(this);
addBtn = (Button) this.findViewById(R.id.appendRadio);
addBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//创建RadioButton对象
/*为什么this做参数需要加前缀
* 因为这个方法存在于一个内部类中,所以直接this指向的是内部类的实例
* 此处需要引用一个上下文对象的实例,而其外部类UITest3Activity继承了Activity的类
* Activity又是继承了上下文Context的类,因此UITest3Activity.this
* 就是这里所需要的上下文对象
* */
RadioButton newRb = new RadioButton(UITest3Activity.this);
newRb.append("不明");
newRb.setId(100);
//添加到RadioGroup中
rg.addView(newRb);
}
});
}
//覆盖OnCheckedChangeListener接口的抽象方法
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(group.getId() == R.id.sexRg){
RadioButton rb = (RadioButton) this.findViewById(checkedId);
Log.i(TAG, rb.getText().toString());
}
}
}
显示效果为:
2)CheckBox
在checkbox_layout.xml中代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="爱好"
android:textSize="20dp"
/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="@+id/tableLayout"
>
<TableRow >
<CheckBox
android:id="@+id/cb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="足球"
/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="篮球"
/>
<CheckBox
android:id="@+id/cb3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="游泳"
/>
<CheckBox
android:id="@+id/cb4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="武术"
/>
</TableRow>
</TableLayout>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/>
</LinearLayout>
在CheckBoxDemo.java中代码如下:
package cn.class3g.activity;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class CheckBoxDemo extends Activity implements OnCheckedChangeListener{
private CheckBox cb1,cb2,cb3,cb4;
private Button submitBtn;
private ArrayList<CheckBox> list = new ArrayList<CheckBox>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox_layout);
findViews();
}
private void findViews() {
cb1 = (CheckBox) this.findViewById(R.id.cb1);
cb2 = (CheckBox) this.findViewById(R.id.cb2);
cb3 = (CheckBox) this.findViewById(R.id.cb3);
cb4 = (CheckBox) this.findViewById(R.id.cb4);
list.add(cb1);
list.add(cb2);
list.add(cb3);
list.add(cb4);
for(CheckBox cb : list){
/*为什么用this
* 当前类实现了监听器接口,所以当前类的实例this可以当做
* 一个监听器对象放入setOnCheckedChangeListener()
* 方法之中做参数*/
cb.setOnCheckedChangeListener(this);
}
submitBtn = (Button) this.findViewById(R.id.submit);
submitBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String fav = "";
for(CheckBox cb : list){
if(cb.isChecked()){
fav += cb.getText() + ",";
}
}
Log.i("TAG", fav);
}
});
}
//覆盖CompoundButton.OnCheckChangeListener接口的抽象方法
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("TAG",buttonView.getText().toString());
}
}
显示结果为: