Android之checkbox使用

来源:互联网 发布:java多线程异常处理 编辑:程序博客网 时间:2024/04/30 04:52

在布局中加上CheckBox标签,给每一个CheckBox注册单独的事件。定义单击事件处理Checkbox

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.android_checkbox.MainActivity" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="个人爱好:"        android:textSize="20sp" />    <CheckBox        android:id="@+id/checkBox1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/textView1"        android:layout_marginLeft="24dp"        android:layout_marginTop="10dp"        android:onClick="onCheckboxClicked"        android:text="上网" />    <CheckBox        android:id="@+id/checkBox2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/checkBox1"        android:layout_marginLeft="24dp"        android:layout_marginTop="15dp"        android:onClick="onCheckboxClicked"        android:text="听歌" />    <CheckBox        android:id="@+id/checkBox3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/checkBox2"        android:layout_marginLeft="24dp"        android:layout_marginTop="20dp"        android:onClick="onCheckboxClicked"        android:text="看电影" />    <CheckBox        android:id="@+id/checkBox4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/checkBox3"        android:layout_marginLeft="24dp"        android:layout_marginTop="25dp"        android:onClick="onCheckboxClicked"        android:text="打游戏" /></RelativeLayout>

package com.example.android_checkbox;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.Toast;public class MainActivity extends ActionBarActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}//注册监听事件onCheckboxClicked事件public void onCheckboxClicked(View view){//判断哪一个CheckBox被选中CheckBox chexkBox=((CheckBox)view);boolean isCheck=chexkBox.isChecked();switch(view.getId()){case R.id.checkBox1:if(isCheck){//可以放到一个容器中,存储复选框选择的itemToast.makeText(MainActivity.this, "checkBox1"+chexkBox.getText(), 1).show();}else{//如果不选中,则从复选框中删除item}break;case R.id.checkBox2:if(isCheck){Toast.makeText(MainActivity.this, "checkBox2"+chexkBox.getText(), 1).show();}else{}break;case R.id.checkBox3:if(isCheck){Toast.makeText(MainActivity.this, "checkBox3"+chexkBox.getText(), 1).show();}else{}break;case R.id.checkBox4:if(isCheck){Toast.makeText(MainActivity.this, "checkBox4"+chexkBox.getText(), 1).show();}else{}break;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic 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();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}

运行效果图


0 0