Android学习笔记之CheckBox

Android学习笔记之CheckBox

CheckBox复选按钮是一种有双状态按钮的特殊类型,可以选中或者不选中。可以现在布局文件中定义多选按钮,然后对每一个多选按钮进行事件监setOnCheckedChangeListener,通过isChecked来判断选项是否被选中

Android学习笔记之CheckBox_第1张图片

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(); } } }); 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学习笔记之CheckBox)