package com.chaowen;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class Ex04_04_checkBox extends Activity {
/** Called when the activity is first created. */
private TextView myTextView1;
private TextView myTextView2;
private CheckBox myCheckBox;
private Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView1=(TextView)findViewById(R.id.myTextView1);
myTextView2=(TextView)findViewById(R.id.myTextView2);
myCheckBox=(CheckBox)findViewById(R.id.myCheckBox);
myButton=(Button)findViewById(R.id.myButton);
//将CheckBox,Button默认为未选择状态
myCheckBox.setChecked(false);
myButton.setEnabled(false);
myCheckBox.setOnClickListener(new CheckBox.OnClickListener(){
@Override
public void onClick(View v) {
if(myCheckBox.isChecked()){
//设置Button为不能选择对象
myButton.setEnabled(true);
myTextView2.setText("");
}else {
//设置Button为可以选择对象
myButton.setEnabled(false);
myTextView1.setText(R.string.text1);
/*CharSequence hint=getString(R.string.hello);*/
/*myCheckBox.setHint(hint);
myCheckBox.setHintTextColor(Color.RED);*/
//在TextView2里显示出"请勾选我同意"
myTextView2.setText(R.string.no);
}
}
});
myButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
if(myCheckBox.isChecked()){
myTextView1.setText(R.string.ok);
}else {
}
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!--建立第一個TextView -->
<TextView
android:id="@+id/myTextView1"
android:layout_width="185px"
android:layout_height="267px"
android:layout_x="67px"
android:layout_y="53px"
android:text="@string/text1"
/>
<!--建立第二個TextView -->
<TextView
android:id="@+id/myTextView2"
android:layout_width="100px"
android:layout_height="30px"
android:layout_x="190px"
android:layout_y="325px"
/>
<!--建立一個CheckBox -->
<CheckBox
android:id="@+id/myCheckBox"
android:layout_width="97px"
android:layout_height="wrap_content"
android:text="@string/str_agree"
android:layout_x="99px"
android:layout_y="318px"
/>
<!--建立一個Button -->
<Button
android:id="@+id/myButton"
android:layout_width="85px"
android:layout_height="wrap_content"
android:text="@string/str_go"
android:layout_x="102px"
android:layout_y="363px"
/>
</AbsoluteLayout>
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Ex04_04_checkBox!</string>
<string name="app_name">Ex04_04_checkBox</string>
<string name="str_agree">我同意</string>
<string name="str_go">確定</string>
<string name="ok">你已接受同意!!</string>
<string name="no">*請勾選我同意</string>
<!-- 空格都是用TAB鍵做區隔的 -->
<string name="text1">我是範例合約~~ 我是範例合約~~ 我是範例合約~~ 我是範例合約~~ 我是範例合約~~</string>
</resources>