返回列表 發帖

Android App - radioGroup

以下程式可以

(1) 在程式內即時加一個新radio button (snack) 放在現有的 radio buttons 之間的指定位置
(2) 按鍵清除 radio group 內所有 selection
(3) Select 任何一個r adio button 會顯示它的  ID



Java 檔案內容

package sample.radiogpadvance;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


public class RadioGroupAdvanceActivity extends Activity implements RadioGroup.OnCheckedChangeListener,
        View.OnClickListener {

    private TextView mChoice;
    private RadioGroup mRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        mRadioGroup = (RadioGroup) findViewById(R.id.menu);

        // test adding a radio button programmatically
        RadioButton newRadioButton = new RadioButton(this);
        newRadioButton.setText(R.string.radio_group_snack);
        newRadioButton.setTextColor(Color.RED);
        newRadioButton.setId(999999);
        // 以下兩句可決定新加入的radio button 放在那那個位置
        LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT);
        mRadioGroup.addView(newRadioButton, 1, layoutParams); // 首位為0, 1為第二位
        // 如以上兩包改為下一包, 則只會放在radio group 最後的位置
        // RadioGroup.addView(newRadioButton);


        mRadioGroup.check(newRadioButton.getId()); // 把新加入的 newRadioButton 變為Selected
        // test listening to checked change events
        String selection = getString(R.string.radio_group_selection); // res/strings.xml 找到預先定義的文字放入 selection
        mRadioGroup.setOnCheckedChangeListener(this);

        // test clearing the selection
        Button clearButton = (Button) findViewById(R.id.clear);
        clearButton.setOnClickListener(this);
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        String selection = getString(R.string.radio_group_selection); // res/strings.xml 找到預先定義的文字放入 selection
        String none = getString(R.string.radio_group_none);            // res/strings.xml 找到預先定義的文字放入 none
        mChoice = (TextView) findViewById(R.id.choice);      
        mChoice.setText(selection + "  "+ (checkedId == View.NO_ID ? none : checkedId));

    }
    // clear any selection within radio group
    public void onClick(View v) {
        mRadioGroup.clearCheck();
    }
}


main.xml 檔案內容
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:checkedButton="@+id/lunch"
        android:id="@+id/menu">
        <RadioButton
            android:text="@string/radio_group_1_breakfast"
            android:id="@+id/breakfast"
            />
        <RadioButton
            android:text="@string/radio_group_1_lunch"
            android:id="@id/lunch" />
        <RadioButton
            android:text="@string/radio_group_1_dinner"
            android:id="@+id/dinner" />
        <RadioButton
            android:text="@string/radio_group_1_all"
            android:id="@+id/all" />
        <TextView
            android:text="@string/radio_group_1_selection"
            android:id="@+id/choice" />
    </RadioGroup>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_clear"
        android:id="@+id/clear" />
</LinearLayout>

Strings.xml 檔案內容
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, RadioGroupAdvanceActivity!</string>
    <string name="app_name">RadioGroupAdvance</string>
    <string name="radio_group_1_breakfast">Breakfast</string>
    <string name="radio_group_1_lunch">Lunch</string>
    <string name="radio_group_1_dinner">Dinner</string>
    <string name="radio_group_1_selection">Selection</string>
    <string name="radio_group_1_all">All</string>
    <string name="radio_group_1_clear">Clear</string>
    <string name="radio_group_snack">Snack</string>
    <string name="radio_group_none">None</string>
    <string name="radio_group_selection">Selection</string>
    <string name="snack">snack</string>

</resources>


附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊
Bill Tang     MSN:billtang@openplatform.com.hk
Openplatform Technology Co.,Ltd. 資訊坊科技有限公司  
無線工程施工、方案設計、無線產品、天饋材料、終端設備綜合供應商
Tel: 852-27491011  Fax: 852-81483532

利用 Radio Group, Group 內所有選項只有從上而下排列, 要自己排列Radio 選項, 亦不能用 Radio Group, 但編程上要花點功夫, 可看看下面的例子



Java 檔內容

package sample.radogroup;

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

public class RadiogroupActivity extends Activity {

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final RadioButton radiofood1 = (RadioButton) findViewById(R.id.radio1);
        final RadioButton radiofood2 = (RadioButton) findViewById(R.id.radio2);
        final RadioButton radiofood3 = (RadioButton) findViewById(R.id.radio3);        
        final RadioButton radiofood4 = (RadioButton) findViewById(R.id.radio4);
        final RadioButton radiofood5 = (RadioButton) findViewById(R.id.radio5);   

        
radiofood1.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            radiofood2.setChecked(false);
           radiofood3.setChecked(false);
           
radiofood4.setChecked(false);      
           
radiofood5.setChecked(false);
           Toast.makeText(RadiogroupActivity.this, radiofood1.getText(), Toast.LENGTH_SHORT).show();
       }
     });

         radiofood2.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            radiofood1.setChecked(false);
            radiofood3.setChecked(false);
           
radiofood4.setChecked(false);      
           
radiofood5.setChecked(false);
           Toast.makeText(RadiogroupActivity.this, radiofood2.getText(), Toast.LENGTH_SHORT).show();
       }
     });


         radiofood3.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            radiofood1.setChecked(false);
            radiofood2.setChecked(false);
           
radiofood4.setChecked(false);      
           
radiofood5.setChecked(false);
           Toast.makeText(RadiogroupActivity.this, radiofood3.getText(), Toast.LENGTH_SHORT).show();
       }
     });


         radiofood4.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            radiofood1.setChecked(false);
            radiofood2.setChecked(false);
           
radiofood3.setChecked(false);      
           
radiofood5.setChecked(false);
           Toast.makeText(RadiogroupActivity.this, radiofood4.getText(), Toast.LENGTH_SHORT).show();
       }
     });


         radiofood5.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            radiofood1.setChecked(false);
            radiofood2.setChecked(false);
           
radiofood3.setChecked(false);      
           
radiofood4.setChecked(false);
           Toast.makeText(RadiogroupActivity.this, radiofood5.getText(), Toast.LENGTH_SHORT).show();
       }
     });


  }}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

    <FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.55" >

        <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/radio1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="@string/radiobutton1"
                    android:textSize="18dip" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/radio2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/radiobutton2"
                    android:textSize="18dip" />

                <RadioButton
                    android:id="@+id/radio3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/radiobutton3"
                    android:textSize="18dip" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/radio4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/radiobutton4"
                    android:textSize="18dip" />

                <RadioButton
                    android:id="@+id/radio5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/radiobutton5"
                    android:textSize="18dip" />
            </LinearLayout>
        </LinearLayout>
</FrameLayout>
</LinearLayout>


Stringx.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Food</string>
    <string name="radiobutton1">Cake</string>
    <string name="radiobutton2">Biscuit</string>
    <string name="radiobutton3">Chocolate</string>
    <string name="radiobutton4">Ice-cream</string>
    <string name="radiobutton5">Sweet</string>
</resources>
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊
Bill Tang     MSN:billtang@openplatform.com.hk
Openplatform Technology Co.,Ltd. 資訊坊科技有限公司  
無線工程施工、方案設計、無線產品、天饋材料、終端設備綜合供應商
Tel: 852-27491011  Fax: 852-81483532

TOP

基本操作
(1) 在同一組中選擇第一個option,

RadioButton opt1 = (RadioButton) findViewById(R.id.radio0);
RadioButton opt2 = (RadioButton) findViewById(R.id.radio1);
opt1 .setChecked(true);
opt2 .setChecked(false);

(2) 檢查那一個option 被選了
if (opt1 .isChecked()) {......}
Bill Tang     MSN:billtang@openplatform.com.hk
Openplatform Technology Co.,Ltd. 資訊坊科技有限公司  
無線工程施工、方案設計、無線產品、天饋材料、終端設備綜合供應商
Tel: 852-27491011  Fax: 852-81483532

TOP

返回列表