【问题标题】:Android: Using RadioGroup in a GridviewAndroid:在 Gridview 中使用 RadioGroup
【发布时间】:2013-02-19 06:47:05
【问题描述】:

我想在 GridView 中布置几十个单选按钮。 现在,我通过创建自定义适配器并覆盖 getView 函数(然后在 GridView 上调用 setAdapter)来实现这一点。 唯一的问题是,我无法将这些单选按钮包含在 RadioGroup 中,因此我可以强制单选按钮。

我查看了各种 stackoverflow 帖子(例如这个:Radio Group implementation on Grid View in Android),但我还没有找到一种直接的方法来完成这项工作。我曾尝试在我的主 XML 中的 GridView 标记周围添加 RadioGroup 标记,但这不起作用。

到目前为止,这是我的一些相关代码:

    private class RadioGridAdapter extends BaseAdapter{
    private Context mContext;
    RadioGridAdapter(Context c) {
        mContext = c;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;
        if(convertView == null) {
            gridView = inflater.inflate(R.layout.cell, null);

            //set value into Button
            RadioButton ButtonView = (RadioButton) gridView.findViewById(R.id.interval_button);
            ButtonView.setText(intervals[position]);

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }
}

cell.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
    android:id="@+id/interval_button" 
    android:layout_width="match_parent"
   android:layout_height="match_parent" >
</RadioButton> 
</LinearLayout>

activity_main.xml:

<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >

<GridView
    android:id="@+id/gridview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="20dp"
    android:numColumns="auto_fit"
    android:paddingLeft="15sp"
    android:paddingRight="15sp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" >
</GridView>

</LinearLayout>

谢谢!

更新: 根据这篇文章(radio button multiple rows)和这篇文章(How to group a 3x3 grid of radio buttons?),如果不扩展 RadioGroup 类以使其能够以网格格式包装单选按钮,则没有一种简单的方法可以做到这一点。有人知道以这种方式使用 RaioGroup 的开源项目吗?

【问题讨论】:

    标签: java android gridview adapter radio-group


    【解决方案1】:

    您可以使用 ListView 代替 GridView。

    如需帮助,请参考我的 Android 应用博客

    http://amitandroid.blogspot.in/2013/03/android-listview-with-radiogroup.html

    希望这篇博文能对你有所帮助。

    非常感谢,

    【讨论】:

    • 感谢您的回复。这会让所有这些单选按钮都在一个 RadioGroup 中吗?如果是这样,你能详细说明你是如何得到这个的吗?
    • 您可以参考我稍后发布的链接。在那个链接(Android 博客)中,我编写了步骤并在底部发布了完整的源代码。
    • 我尝试运行源代码,但 Eclipse 告诉我“无法解析目标 'Google Inc.:Google APIs:8'” ...我猜我们的开发环境太不一样了?我还是 Eclipse/Android 编程的新手,所以我不确定为什么它不起作用。此外,您在这里的每行似乎有一个 RadioGroup,但我想为整个网格设置一个 RadioGroup。 ListView 还可以吗?
    • 我知道你为什么会收到这个错误。我选择 google Api 进行编译。因此,您需要从 AVD 列表中创建一个应该是 Google Api 的 AVD。然后在那个google api AVD中运行我的源代码,它应该运行。正如您所问的那样,整个 GridView 的 RadioGroup 是不可能的,因为它可能是水平或垂直方式。不是两种方式。在 ListView 中,每个项目都有一个水平 RadioGroup。因此,您可以轻松识别检查了哪一行和哪个 RadioButton。谢谢。
    • 嗯,感谢您的帮助,但不幸的是,这不是我想要的...我希望整个网格成为一个 RadioGroup。我只是觉得很奇怪,没有一种简单的方法可以实现这一点,因为它不是布置单选按钮的晦涩方式
    猜你喜欢
    • 2017-12-20
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 2023-03-18
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多