Android学习笔记之CheckBox
CheckBox复选按钮是一种有双状态按钮的特殊类型,可以选中或者不选中。可以现在布局文件中定义多选按钮,然后对每一个多选按钮进行事件监setOnCheckedChangeListener,通过isChecked来判断选项是否被选中main.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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/theme" />
<CheckBox
android:id="@+id/apple"
android:text="@string/apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/banana"
android:text="@string/banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
RadioGroupActivity.java
package Android.Activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class RadioGroupActivity extends Activity {
/** Called when the activity is first created. */
private CheckBox appleCheck = null;
private CheckBox bananaCheck = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过控件的ID来得到代表控件的对象
appleCheck = (CheckBox)findViewById(R.id.apple);
bananaCheck = (CheckBox)findViewById(R.id.banana);
//为RadioGroup设置易做图,需要注意的是,这里的易做图和Button控件的易做图有所不同
appleCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(RadioGroupActivity.this,"恭喜你,还剩很多苹果", Toast.LENGTH_LONG).show();
}
}
}); www.zzzyk.com
bananaCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(RadioGroupActivity.this,"恭喜你,还剩很多香蕉", Toast.LENGTH_LONG).show();
}
}
});
}
}
摘自 落日小屋
补充:移动开发 , Android ,