【问题标题】:How to make sure that only single radiobutton is selected in childview of expandable list view in android?如何确保在 android 的可扩展列表视图的子视图中只选择单个单选按钮?
【发布时间】:2015-12-13 09:29:52
【问题描述】:

我在我的应用程序及其子视图中使用可扩展列表视图,我正在使用单选按钮的自定义列表。这里的问题是我想确保一次只能选择一个单选按钮。如果一个单选按钮被选中,其他应该被取消选择。这是我正在使用的代码:

@Override
    public View getChildView(int groupPosition,int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent)
    {
        m_position = childPosition;

        View l_view = convertView;
        if (l_view == null) 
        {
            LayoutInflater l_vi = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            l_view = l_vi.inflate(R.layout.singleradiobutton, null);

            ViewHolder viewHolder = new ViewHolder();
            viewHolder.l_nameRadioButton = (RadioButton) l_view.findViewById(R.id.radioButton);

            l_view.setTag(viewHolder);
        }

        EnteringValuesToArrayList l_enteringValuesInstance = m_itemsOfAdapterArrayList.get(childPosition);
        String l_nameOfContact = l_enteringValuesInstance.GetNameOfEntry();

        if (l_nameOfContact != null)
        {
            ViewHolder holder = (ViewHolder) l_view.getTag();

            if (holder.l_nameRadioButton != null) 
            {
                holder.l_nameRadioButton.setText(l_nameOfContact);
            }

            if(m_timerValue != 0 && l_enteringValuesInstance.GetTimerValueOfEntry() == m_timerValue)
                holder.l_nameRadioButton.setChecked(true);
            else
                holder.l_nameRadioButton.setChecked(false); 

            holder.l_nameRadioButton.setOnClickListener(new OnItemClickListener(childPosition,holder.l_nameRadioButton.getText(),holder.l_nameRadioButton));          
        }
        return l_view;
    }

    private class OnItemClickListener implements OnClickListener
    {           
        private int l_positionOfItemClicked;
        private CharSequence l_text;
        private RadioButton l_radioButton;
        OnItemClickListener(int position, CharSequence text,RadioButton l_nameRadioButton)
        {
            this.l_positionOfItemClicked = position;
            this.l_text = text;
            this.l_radioButton = l_nameRadioButton;
        }
        @Override
        public void onClick(View arg0) 
        {
            l_radioButton.setChecked(true); 
        }
    }

我搜索了很多,但没有得到问题的解决方案。请帮助我。提前谢谢。

【问题讨论】:

标签: android radio-button selection


【解决方案1】:

How to use RadioGroup in ListView custom adapter? 中提出了类似的问题

您可以根据自己的情况创建自定义视图。

希望这会有所帮助。

【讨论】:

  • 我检查并尝试了解决方案,但它们都不适用于我的情况。
【解决方案2】:

您需要在RadioGroup 中包含radioButtons。当组合在一起时,每个RadioGroup 一次只能选择一个按钮。否则,它们都可以被选中或不选中。 请参阅有关它的文档Here

【讨论】:

  • 如何在可扩展列表视图的自定义子视图中使用单选组?
  • RadioGroup 添加到您的 xml 中,并附上所需的 RadioButtons。然后就可以根据需要使用文档中的功能了
【解决方案3】:

使用 RadioGroup 代替,因为 RadioGroup“用于将一个或多个 RadioButton 视图组合在一起,从而只允许在 RadioGroup 中检查一个 RadioButton”

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rbtGp1);
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton rb1 = (RadioButton) findViewById(R.id.rbt1);
            if (rb1.isChecked()) {
                // Do something
            else {
                // Do something else
        }
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 2014-06-19
    • 2018-09-19
    • 2014-03-13
    • 1970-01-01
    相关资源
    最近更新 更多