强制radiobutton只选择一个值

时间:2015-05-11 06:56:47

标签: android android-listview radio-button

我在LayoutInflater中使用radiobutton,目前它正在选择视图中的所有值,我想在listview中只选择一个,我该怎么做?

工作代码:

MainActivity.java

package com.androidhub4you.multilevellistview;

import java.util.ArrayList;

/**
 * 
 * first level item
 * 
 */
public class Product {

    private String pName;

    private ArrayList<SubCategory> mSubCategoryList;

    public Product(String pName, ArrayList<SubCategory> mSubCategoryList) {
        super();
        this.pName = pName;
        this.mSubCategoryList = mSubCategoryList;
    }

    public String getpName() {
        return pName;
    }

    public void setpName(String pName) {
        this.pName = pName;
    }

    public ArrayList<SubCategory> getmSubCategoryList() {
        return mSubCategoryList;
    }

    public void setmSubCategoryList(ArrayList<SubCategory> mSubCategoryList) {
        this.mSubCategoryList = mSubCategoryList;
    }

    /**
     * 
     * second level item
     * 
     */

    public static class SubCategory {

        private String pSubCatName;
        private ArrayList<ItemList> mItemListArray;

        public SubCategory(String pSubCatName,
                ArrayList<ItemList> mItemListArray) {
            super();
            this.pSubCatName = pSubCatName;
            this.mItemListArray = mItemListArray;
        }

        public String getpSubCatName() {
            return pSubCatName;
        }

        public void setpSubCatName(String pSubCatName) {
            this.pSubCatName = pSubCatName;
        }

        public ArrayList<ItemList> getmItemListArray() {
            return mItemListArray;
        }

        public void setmItemListArray(ArrayList<ItemList> mItemListArray) {
            this.mItemListArray = mItemListArray;
        }

        /**
         * 
         * third level item
         * 
         */
        public static class ItemList {

            private String itemName;
            private String itemPrice;

            public ItemList(String itemName, String itemPrice) {
                super();
                this.itemName = itemName;
                this.itemPrice = itemPrice;
            }

            public String getItemName() {
                return itemName;
            }

            public void setItemName(String itemName) {
                this.itemName = itemName;
            }

            public String getItemPrice() {
                return itemPrice;
            }

            public void setItemPrice(String itemPrice) {
                this.itemPrice = itemPrice;
            }

        }

    }

}

Product.java

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

</ScrollView>

activity_main.xml中

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

        <TextView
            android:id="@+id/textViewItemName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="TextView"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/textViewItemPrice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="TextView"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

  <RadioButton
    android:id="@+id/selection"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4px"
    android:layout_marginRight="10px"
    android:layout_alignParentRight="true"
    />


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray" />

    </LinearLayout>

row_third.xml

{{1}}

3 个答案:

答案 0 :(得分:0)

    <?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pirates"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ninjas"
        android:onClick="onRadioButtonClicked"/>
</RadioGroup>

单击单选按钮

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_pirates:
            if (checked)
                // Pirates are the best
            break;
        case R.id.radio_ninjas:
            if (checked)
                // Ninjas rule
            break;
    }
}

答案 1 :(得分:0)

如果检查了任何radioButton,您可以以编程方式检查,并在for循环中使用 OVRManager.display.isDirectMode OVRManager.display.isPresent 检查所选的一个之前取消选中它。

答案 2 :(得分:-1)

RadioButton控件放在RadioGroup

正如链接文档所述,RadioGroup会自动处理:

  

此类用于为一组单选按钮创建多重排除范围。选中一个属于无线电组的单选按钮,取消选中同一组中之前检查过的任何单选按钮。