Android 控件:CheckBox
来源:互联网 发布:Windows测试udp 编辑:程序博客网 时间:2024/04/30 05:54
首先,在布局文件中注册CheckBox
activity_main.xml
<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" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/eatID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="吃饭" /> <CheckBox android:id="@+id/sleepID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="睡觉"/> <CheckBox android:id="@+id/dotaID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dota"/></LinearLayout>
MainActivity.java
CheckBox 即可以绑定 view.OnClickListener,也可以绑定 CompoundButton.OnCheckedChangeListener.
package com.example.litingdong.checkboxtest;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.CheckBox;import android.widget.CompoundButton;public class MainActivity extends ActionBarActivity { private CheckBox eatBox; private CheckBox sleepBox; private CheckBox dotaBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); eatBox=(CheckBox)findViewById(R.id.eatID); sleepBox=(CheckBox)findViewById(R.id.sleepID); dotaBox=(CheckBox)findViewById(R.id.dotaID); //OnClickListener OnButtonClickListener listener=new OnButtonClickListener(); listener = new OnButtonClickListener(); eatBox.setOnClickListener(listener); sleepBox.setOnClickListener(listener); dotaBox.setOnClickListener(listener); //OnCheckedChangedListener CheckBoxListener changeListener =new CheckBoxListener(); eatBox.setOnCheckedChangeListener(changeListener); sleepBox.setOnCheckedChangeListener(changeListener); dotaBox.setOnCheckedChangeListener(changeListener); } class OnButtonClickListener implements View.OnClickListener{ @Override public void onClick(View v) { CheckBox box=(CheckBox)v; if (box.getId()==R.id.eatID) { System.out.println("eatBox"); }else if(box.getId()==R.id.sleepID){ System.out.println("sleepBox"); }else{ System.out.println("dotaBox"); } if (box.isChecked()){ System.out.println("Checked!"); }else{ System.out.println("UnChecked!"); } } } class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (buttonView.getId()==R.id.eatID){ System.out.println("eatBox"); }else if (buttonView.getId()==R.id.sleepID){ System.out.println("sleepBox"); }else{ System.out.println("dotaBox"); } if (isChecked){ System.out.println("Checked!"); }else{ System.out.println("UnChecked!"); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
logcat:
1 0
- android控件08 CheckBox
- Android 控件:CheckBox
- Android控件之checkbox
- Android控件CheckBox学习
- Android控件之CheckBox
- Android 控件之checkBox
- Android控件---CheckBox
- Android控件之CheckBox
- Android控件CheckBox、RadioButton
- android控件之CheckBox
- Android控件之CheckBox
- android控件06---CheckBox
- CheckBox---Android控件系列之CheckBox
- Android控件开发之CheckBox
- android控件之radiobutton checkbox
- android 控件 多项选择(CheckBox)
- Android常见控件Radio Checkbox
- Android控件——CheckBox
- ORA-15025: could not open disk "/dev/asm***"
- 使用setjmp()和longjmp()执行非局部跳转
- Windows下MongoDB安装和配置
- Casperjs中fill提交表单遇到的问题
- windows程序设计——飞机大战笔记(解决输入混合密码账户,结局自动记录数据)
- Android 控件:CheckBox
- 【从零开始学NGUI 】 (七)Atlas制作
- 水平凡 创建新的插件
- Android面试---Handler,Looper,MessageQueue
- Html5中的<section>标签
- 加载配置文件
- ZOJ_1091-Knight Moves
- 巡逻机器人(Patrol Roboot,UVa1600)
- Unity3D-client +MinaServer仿微信聊天系统