Android控件笔记——CheckBox复选框
16lz
2021-01-23
1、CheckBox的两种状态:
选中状态(true),未选中状态(false);
2、属性:
android:id="@+id/checkbox"
android:layout_width="warp_content"
android:layout_height="warp_content"
android:checked="false"
android:text="男"
3、应用:
拖入CheckBox控件,并对它进行相关设置:
<!--activity_main.xml--> <CheckBox android:checked="false" android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你是猪" />
初始化CheckBox,并设置监听器监听他的状态,并根据对应状态输出提示信息:
public class MainActivity extends Activity {//定义checkBoxprivate CheckBox checkBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化checkBox checkBox=(CheckBox) findViewById(R.id.checkBox1); //通过设置checkBox的监听事件来对checkBox是不是被选中 checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { //通过匿名内部类来实现监听器@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stub//通过onCheckChanged来监听当前的checkBox是否被选中if(arg1){//获得checkBox的文本内容String text="对!"+checkBox.getText().toString();Toast.makeText(MainActivity.this, text, 1).show();}else{Toast.makeText(MainActivity.this, "不对,你是猪!", 1).show();}}}); } }
4、效果:
更多相关文章
- Android 布局----让一个控件居底部
- Android中控件的隐藏和显示
- 各种控件
- 用EditText控件的属性inputType
- Android Studio控件属性大全
- Android 控件xml属性详解
- Android:控件布局(相对布局)RelativeLayout内摆放
- ANDROID:控件属性(很全)
- android 使控件透明