本人Android新手
//MainActivity.java
package com.hipad.checkboxtext;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
public class MainActivity extends ActionBarActivity {
private CheckBox eat = null;
private CheckBox sleep = null;
private CheckBox all = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
eat = (CheckBox)findViewById(R.id.eat);
System.out.println(R.id.eat+"...");
sleep = (CheckBox)findViewById(R.id.sleep);
all = (CheckBox)findViewById(R.id.all);
OnclickCheckBox check = new OnclickCheckBox();
System.out.println(check);
if(eat == null)
System.out.println("eat=null!");
if(check == null)
System.out.println("check=null");
eat.setOnClickListener(check);
//sleep.setOnClickListener(check);
//all.setOnClickListener(check);
}
@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);
}
//实现onclick方法
class OnclickCheckBox implements OnClickListener{
@Override
public void onClick(View v) {
CheckBox box = (CheckBox)v;
if(box.getId() == R.id.eat){
System.out.println("i am eating");
}
else if(box.getId() == R.id.sleep){
System.out.println("i am sleeping");
}
else{
}
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
//res.fragment_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="com.hipad.checkboxtext.MainActivity$PlaceholderFragment" >
<CheckBox
android:id="@+id/eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="eat"
/>
<CheckBox
android:id="@+id/sleep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="slepp"
/>
<CheckBox
android:id="@+id/all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="all"/>
</LinearLayout>
//activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"