如何设置单选按钮,选中当我在列表项点击 [英] How to set radio button to checked when i click on list item

查看:217
本文介绍了如何设置单选按钮,选中当我在列表项点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的列表视图(TextView的2和1个单选按钮)有问题。

I am having a problem with my listview ( 2 textview and 1 radio button).

问题:
我的想法是,用户点击该项目的ListView和单选按钮被自动选中。

我一直在寻找了一段时间,但我不能让单选按钮的工作。

I have been searching for a while, but I can't get the radio button to work.

我的XML

   <RadioButton
        android:id="@+id/rdBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false" />

我的适配器

    r = (RadioButton) convertView.findViewById(R.id.rdBtn);
        r.setChecked(selectedPosition == position);
        r.setTag(position);
        r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //r.setChecked(true);
                Toast.makeText(context, InformationActivity.result,
                        Toast.LENGTH_SHORT).show();
                selectedPosition = (Integer) view.getTag();
                notifyDataSetInvalidated();

            }
        });
        return convertView;

我试过

r.setChecked(true);

里面我的活动类和第一次点击的工作,但第二个选择的列表视图不同的项目。

inside my activity class and the first click worked, but the second chooses a different item on the listview.

我希望你们当中有些人能帮助我。
谢谢

I hope some of you can help me. Thanks

推荐答案

我发现这里的解决方案:
如何检查复选框上点击图像?

I found the solution here: How to check checkbox on clicking an image?

        searchList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                int position, long arg3) {

            LinearLayout item_view = (LinearLayout) view;
            final RadioButton itemcheck = (RadioButton) item_view
                    .findViewById(R.id.rdBtn);

            if (itemcheck.isChecked()) {
                itemcheck.setChecked(true);
            } else {
                itemcheck.setChecked(false);
            }

            itemcheck.setChecked(true);


<RadioButton
        android:id="@+id/rdBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false" />

这篇关于如何设置单选按钮,选中当我在列表项点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆