2009年12月23日星期三

RadioButton

跟CheckBox一樣, RadioButton可以是及必需是兩種狀態中的其中一種狀態, checked或unchecked. 但checked了之後, 用戶無法把它uncheck.

RadioButton

創建一個Android Application, AndroidRadioButton. 並根據下面的例子修改佈局文件(/res/layout/main.xml)及主程序(AndroidRadioButton.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"
/>
<RadioButton
android:id="@+id/radiobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button"
/>
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
/>
</LinearLayout>


AndroidRadioButton.java

package com.AndroidRadioButton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class AndroidRadioButton extends Activity {

RadioButton radioButton;
Button clearButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

radioButton = (RadioButton)findViewById(R.id.radiobutton);
clearButton = (Button)findViewById(R.id.clear);
clearButton.setOnClickListener(clearButtonOnClickListener);
}

private Button.OnClickListener clearButtonOnClickListener
= new Button.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
radioButton.setChecked(false);
}

};
}



相關文章: RadioGroup

沒有留言:

發佈留言