CheckBox的應用

摘要:CheckBox應用

核取CheckBox並顯示CheckBox的項目:

package tw.nkfust.jason;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class CheckboxSampleActivity extends Activity {
    /** Called when the activity is first created. */
 TextView txtview2;
 CheckBox cbx1;
 CheckBox cbx2;
 CheckBox cbx3;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        txtview2 = (TextView) findViewById(R.id.textView2);
        cbx1 = (CheckBox) findViewById(R.id.checkBox1);
        cbx2 = (CheckBox) findViewById(R.id.checkBox2);
        cbx3 = (CheckBox) findViewById(R.id.checkBox3);
       
        CheckBox.OnCheckedChangeListener cbxchanged = new CheckBox.OnCheckedChangeListener(){
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {
    // TODO Auto-generated method stub
    String str0="你喜歡吃的是:";
    
    if(cbx1.isChecked()){
     str0+=cbx1.getText();
    }
    if(cbx2.isChecked()){
     str0+=cbx2.getText();
    }
    if(cbx3.isChecked()){
     str0+=cbx3.getText();
    }
    txtview2.setText(str0);
   }         
        };
       
        cbx1.setOnCheckedChangeListener(cbxchanged);
        cbx2.setOnCheckedChangeListener(cbxchanged);
        cbx3.setOnCheckedChangeListener(cbxchanged);
    }
}