checkBox判定_第1张图片
package com.yanjun;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity {
   /** Called when the activity is first created. */

  TextView textView;
  Button button;
  CheckBox checkBox;

  @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    textView = (TextView) findViewById(R.id.textView1);
    button = (Button) findViewById(R.id.button1);
    checkBox = (CheckBox) findViewById(R.id.checkBox1);
     //checkBox.setChecked(true);默认已选中
         checkBox.setChecked( true);
    button.setEnabled( false);
    button.setOnClickListener( new OnClickListener() {

       public void onClick(View v) {
         // TODO Auto-generated method stub
         if (checkBox.isChecked()) {
          textView.setText( "点击按钮显示内容");
          textView.setTextColor(Color.BLUE);
        }
      }
    });
    checkBox.setOnClickListener( new OnClickListener() {

       public void onClick(View v) {
         // 判断选中状态
         if (checkBox.isChecked()) {
          button.setEnabled( true);
          textView.setText( "checkBox已选中,显示按钮");
          textView.setTextColor(Color.GREEN);
        } else {
          button.setEnabled( false);
          textView.setText( "checkBox没有选中,按钮变灰");
          textView.setTextColor(Color.RED);
        }
      }
    });

  }
}