android clear radiogroup onclick button(清除)

android clear radiogroup onclick button(clear)

如何在单击按钮时取消选中单选组中的所有单选按钮。可能的代码是什么。试过 radiogroup.clearcheck() - 不工作。 我的无线电组在列表视图中。 从两个不同的布局调用按钮和列表视图。

在按钮上(清除)单击单选按钮应取消选中

查看代码请关注post 提前致谢。

要清除 ListView 中的所有单选按钮,请在您的适配器中 Class ,

RadioButton rb;

rb.setChecked(false);

为所选答案传递任何默认值。 说 selectedAnser = 0;

在您的适配器中,

if(selectedAnswer.equals("0"))
{
//make all your radiobutton to checked false
radioButton.setChecked(false);
}

在您的 Activity 中,单击清除按钮时,

 mClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

              for(int i=0;i<yourDatalist.size();i++)
                  {

                    yourDatalist.get(i).setSelectedAnswer = "0";
                  } 
               //make adapter notify  here
               adapter.notifyDataSetChanged();
            }
        });

使用

 radioGroup.clearCheck();

radioGroup.check(-1);

             for (int i = 0; i < radioGroup.getChildCount(); i++) {
                            RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                            radioButton.setChecked(false);
                        }

use this example

radioGroup = (RadioGroup) findViewById(R.id.radioGroup); radioGroup.clearCheck();

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton rb = (RadioButton) group.findViewById(checkedId);
            if (null != rb && checkedId > -1) {
                Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
            }

        }
    });

}

public void onClear(View v) {
    /* Clears all selected radio buttons to default */
    radioGroup.clearCheck();
}

将您的 CustomAdapter 构造函数更改为:

public CustomAdapter(Context applicationContext, String[] questionsList) {
    this.context = context;
    this.questionsList = questionsList;
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < questionsList.length; i++) {
        selectedAnswers.add("3");
    }
    inflter = (LayoutInflater.from(applicationContext));
}

收件人:

public CustomAdapter(Context applicationContext, String[] questionsList) {
    this.context = context;
    this.questionsList = questionsList;
    resetAnswers();
    inflter = (LayoutInflater.from(applicationContext));
}

public void resetAnswers(){
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < questionsList.length; i++) {
        selectedAnswers.add("3");
    }
}

并在 OnClick() 内部用于 清除 按钮:

customAdapter.resetAnswers();
customAdapter.notifyDataSetChanged();

希望对您有所帮助!