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); } } }); } }