照例贴地址,东西都比较简单。
http://android-doc.com/guide/topics/ui/controls/checkbox.html
在xml下新建几个CheckBox,然后在checkbox下注册onClick即可
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<CheckBox
android1:id="@+id/checkBox2"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_alignLeft="@+id/checkBox1"
android1:layout_below="@+id/checkBox1"
android1:onClick="onCheckboxClicked"
android1:layout_marginTop="45dp"
android1:text="喵" />
<CheckBox
android1:id="@+id/checkBox1"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_alignParentLeft="true"
android1:layout_alignParentTop="true"
android1:layout_marginTop="80dp"
android1:onClick="onCheckboxClicked"
android1:text="汪" />
<CheckBox
android1:id="@+id/checkBox3"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_alignParentLeft="true"
android1:layout_below="@+id/checkBox1"
android1:layout_marginTop="14dp"
android1:onClick="onCheckboxClicked"
android1:text="哼" />
</RelativeLayout>
MainActivity.java
package com.example.tree;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener{
private CheckBox check1;
private CheckBox check2;
private CheckBox check3;
private final String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "-onCreate-->>");
check1=(CheckBox)this.findViewById(R.id.checkBox1);
check2=(CheckBox)this.findViewById(R.id.checkBox2);
check3=(CheckBox)this.findViewById(R.id.checkBox3);
}
public void onCheckboxClicked(View v){
boolean checked = ((CheckBox)v).isChecked();
switch(v.getId()){
case R.id.checkBox1:
if(checked)
Toast.makeText(MainActivity.this, "check1 on", 1).show();
else Toast.makeText(MainActivity.this, "check1 off", 1).show();
break;
case R.id.checkBox2:
if(checked)
Toast.makeText(MainActivity.this, "check2 on", 1).show();
else Toast.makeText(MainActivity.this, "check2 off", 1).show();
break;
case R.id.checkBox3:
if(checked)
Toast.makeText(MainActivity.this, "check3 on", 1).show();
else Toast.makeText(MainActivity.this, "check3 off", 1).show();
break;
}
}
protected void onStart(){
super.onStart();
Log.i(TAG, "-onStart-->>");
}
protected void onRestart(){
super.onRestart();
Log.i(TAG, "-onRestart-->>");
}
protected void onResume(){
super.onResume();
Log.i(TAG, "-onResume-->>");
}
protected void onPause(){
super.onPause();
Log.i(TAG, "-onPause-->>");
}
protected void onStop(){
super.onStop();
Log.i(TAG, "-onStop-->>");
}
protected void onDestroy(){
super.onDestroy();
Log.i(TAG, "-onDestroy-->>");
}
@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;
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}