創建一個Android Application, AndroidCheckBox. 並根據下面的例子修改佈局文件(/res/layout/main.xml)及主程序(AndroidCheckBox.java).
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Check Box"
/>
</LinearLayout>
AndroidCheckBox.java
package com.AndroidCheckBox;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class AndroidCheckBox extends Activity {
CheckBox checkBox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkBox = (CheckBox)findViewById(R.id.checkbox);
checkBox.setOnClickListener(CheckBoxOnClickListener);
}
private CheckBox.OnClickListener CheckBoxOnClickListener =
new CheckBox.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(AndroidCheckBox.this,
"Check Box State: "
+ checkBox.isChecked(),
Toast.LENGTH_LONG).show();
}
};
}
沒有留言:
發佈留言