我的列表视图中有行。在每一行我有两个单选按钮,两个都在同一个无线电组中。
单选按钮名为" Full"和"部分" 。我只想要一个" Partial"要在我的列表视图中的所有行中选择并休息所有需要变为" Full"
因此,当用户选择"部分"在一个,并且之前标记的单选按钮为" partial",它应该更改为" Full"。在这里,我尝试过,但没有得到我想要的输出。
holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i)
{
if(i==R.id.radio_partial_selection)
{
if (selected != null)
{
selected.setChecked(false);
}
holder.radioButton_partial.setChecked(true);
Log.d("selected=","11"+selected);
selected = holder.radioButton_partial;
Log.d("selected=","22"+selected);
}
if(i==R.id.radio_full_selection)
{
}
}
});
答案 0 :(得分:0)
在onCheckedListener之外,我会设置一个在检查其中一个部分按钮时切换的布尔值。然后查看视图以查看是否已抛出部分开关。如果有,请让视图显示“完整”按钮而不是部分按钮。
虽然我不熟悉单选按钮(我没有在android上使用它们), 如果有必要,我可以为你准备一些示例代码。
编辑(提供示例代码)
在您的特定情况下,这就是我处理您的问题的方法。正如其中的评论所述,有一些假设(因为我没有你的代码)
public class sampleRadioButtonActivity extends Activity {
/**
* When this is set to false, all radio buttons will show partial.
* When this is set to true, all radio buttons will show full, except the one that is
* set to show partial
*/
private boolean showPartialOrFull;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/**
* Do your on create stuff here
*/
/**
* We are setting the showPartialOrFull here to be false,
* this will allow the view to show partial or full for now
*/
showPartialOrFull = false;
holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.radio_partial_selection){
showPartialOrFull = true;
changeRadioButtonToFull(holder);
}
}
});
}
/**
* Because I'm missing some code, assumptions are made here.
* The first assumption is that the radio buttons are sitting in a viewgroup.
* The second assumption, is more a recommendation, that you put the radiobuttons
* in a ViewFlipper or ViewSwitcher (I will be using the first).
*/
private final void changeRadioButtonToFull(View viewToIgnore){
for(int i = 0; i < viewGroup.getChildCount(); i++){
View child = viewGroup.getChildAt(i);
if(child != viewToIgnore){
((ViewFlipper) child).setDisplayedChild(R.id.radio_full_selection);
}
}
}