Radio Button物件通常用於"單選"項目
mian.xml
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton
android:id="@+id/radio_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
Xxx.java
public void onCreate(Bundle savedInstanceState) {
.......
RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
radio_red.setOnClickListener(radio_listener);
radio_blue.setOnClickListener(radio_listener);
}
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
// 顯示選擇項目
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};
-------------------------------------------------------------------------------------
private RadioGroup.OnCheckedChangeListener change =
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == mRadio1.getId()) {
Toast.makeText(radio.this, "1", Toast.LENGTH_SHORT).show();
}
if (checkedId == mRadio2.getId()) {
Toast.makeText(radio.this, "2", Toast.LENGTH_SHORT).show();
}
}
};
沒有留言:
張貼留言