1.layout代码
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <CheckBox 12 android:id="@+id/checkBox1" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:layout_alignParentTop="true" 16 android:layout_centerHorizontal="true" 17 android:layout_marginTop="17dp" 18 android:text="篮球" /> 19 20 <CheckBox 21 android:id="@+id/checkBox2" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:layout_alignLeft="@+id/checkBox1" 25 android:layout_below="@+id/checkBox1" 26 android:text="足球" /> 27 28 <CheckBox 29 android:id="@+id/checkBox3" 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:layout_alignLeft="@+id/checkBox2" 33 android:layout_below="@+id/checkBox2" 34 android:text="乒乓球" /> 35 36 </RelativeLayout>
2.源代码
package com.example.text2; import android.os.Bundle; import android.util.Log; import android.accounts.Account; import android.accounts.OnAccountsUpdateListener; import android.app.Activity; import android.view.Menu; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends Activity { private CheckBox CheckBox1; private CheckBox CheckBox2; private CheckBox CheckBox3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CheckBox1 =(CheckBox) findViewById(R.id.checkBox1); CheckBox2 =(CheckBox) findViewById(R.id.checkBox2); CheckBox3 =(CheckBox) findViewById(R.id.checkBox3); CheckBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub if(arg1){ String text = CheckBox1.getText().toString(); Log.i("tag", text); } } }); CheckBox2.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub if (arg1) { String text = CheckBox2.getText().toString(); Log.i("tag",text); } } }); CheckBox3.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub if(arg1){ String text = CheckBox3.getText().toString(); Log.i("tag", text); } } }); } @Override public 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; } }
3.点击足球选项之后,logcat的效果
04-18 22:37:19.921: I/tag(1694): 足球