2014年7月28日 星期一

[Android]多選一的單選鈕 (RadioButton)

  單選按鈕即RadioButton,表示在同一群組內只能選擇一個項目。

RadioButton & RadioGroup元件
  要讓一組 RadioButton每次只能有一個被選,就必須把它們都放置在同一個 RadioGroup中。


如上圖所示,其中就有三個 RadioButton,而三個都被包在同一個 RadioGroup(你看不見)中。


getCheckedRadioButtonId():讀取單選鈕狀態
   由於 RadioButton通常是組合在 RadioGoup之下,因此在程式中要判斷使用者選擇哪一個 RadioButton,可透過 RadioGrop的 getCheckedRadioButtonId()方法,取得被選取的 RadioButton之資源 ID。接著利用判斷式(例: if/else)就可以據以決定程式的走向,其格式如下:

//取得 RadioGroup之物件
RadioGroup Iwant = (RadioGroup) findViewById(...);
    ...
//判斷選擇的項目資源 ID是不是所選擇的該項ID
if(Iwant.getCheckedRadioButtonId() == R.id.takeSword) {
    ...
}
else if(Iwant.getCheckedRadioButtonId() == R.id.cutSoul){
    ...
}
else{
    ...
}

※讀取RadioGroup的選取項目:


MainActivity.java
package tw.com.Mei_Jiang;

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.activity_main, menu);
                return true;
        }
        
        public void show(View v){
                TextView txv=(TextView)findViewById(R.id.txv);
                RadioGroup ticketType =
                                (RadioGroup) findViewById(R.id.ticketType);
                
                // 依選取項目顯示不同訊息
                switch(ticketType.getCheckedRadioButtonId()){
                case R.id.sword:        // 選 拿勝利寶劍
                        txv.setText("拿勝利寶劍");
                        break;
                case R.id.soul:        // 選 斷開魂節
                        txv.setText("斷開魂節");
                        break;
                case R.id.chain:        // 選 斷開鎖練
                        txv.setText("斷開鎖練");
                        break;
                }
        }
}

備註:別忘了,R.id.XXX都是在 R.java中以 final宣告的常數,其值是固定不變的,因此可用在 switch敘述中用來區別選取的單選鈕。


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/txv"
        android:layout_centerHorizontal="true"
        android:onClick="show"
        android:text="確定" />

    <RadioGroup
        android:id="@+id/ticketType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_centerHorizontal="true" >

        <RadioButton
            android:id="@+id/sword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="拿勝利寶劍" />

        <RadioButton
            android:id="@+id/soul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="斷開魂節" />

        <RadioButton
            android:id="@+id/chain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="斷開鎖練" />
    </RadioGroup>

</RelativeLayout>

沒有留言:

張貼留言