package com.example.checkbox;
import android.app.*;
import android.os.*;
import android.widget.*;
import android.widget.CompoundButton.*;
import android.view.Menu;
public class MainActivity extends Activity {
TextView tv;
CheckBox cb1, cb2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(ll);
tv = new TextView(this);
tv.setText("點餐吧。");
cb1 = new CheckBox(this);
cb2 = new CheckBox(this);
cb1.setText("牛肉飯");
cb2.setText("鰻魚飯");
ll.addView(tv);
ll.addView(cb1);
ll.addView(cb2);
cb1.setOnCheckedChangeListener(new SampleCheckedChangeListener());
cb2.setOnCheckedChangeListener(new SampleCheckedChangeListener());
}
class SampleCheckedChangeListener implements OnCheckedChangeListener
{
public void onCheckedChanged(CompoundButton cb, boolean isChecked)
{
if(isChecked == true)
{
tv.setText("我要吃" + cb.getText() + "。");
}
else if(isChecked == false)
{
tv.setText("我不吃" + cb.getText() + "了。");
}
}
}
@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;
}
}