RadioGroup Check(id)不触发android中的setOnCheckedChangeListener

时间:2016-10-06 07:32:31

标签: android android-radiogroup

我有一个radiogroup并动态添加了单选按钮。我使用radiogroup.check(id)来选择Radiogroup中的radiobutton。

v = new RadioGroup(ctx);
int count = 0;
for (String Str : valueStr.get(Position)) {
    RadioButton rb = new RadioButton(ctx);
    rb.setId(count);
    rb.setTag(valueStrID.get(position).get(count));
    rb.setText(Str);
    if (formFieldObject.getFD_ValueField().equals(String.valueOf(Str))) {
        rbposition = count;
   }
   rb.setTextSize(fontSize);
   ((RadioGroup) v).addView(rb);
   count++;
}

并调用此方法进行选择

((RadioGroup) v).check(rbposition);

但这不会触发Checkedchangelistener。代码有什么问题?

3 个答案:

答案 0 :(得分:1)

致电rb.setChecked(true)进行检查。

v = new RadioGroup(ctx);
int count = 0;
for (String Str : valueStr.get(Position)) {
RadioButton rb = new RadioButton(ctx);
rb.setId(count);
rb.setTag(valueStrID.get(position).get(count));
rb.setText(Str);
rb.setTextSize(fontSize);
((RadioGroup) v).addView(rb);
if (formFieldObject.getFD_ValueField().equals(String.valueOf(Str))) {
    rbposition = count;
    rb.setChecked(true);
}


count++;
}

答案 1 :(得分:1)

我的代码工作只是检查。我认为你在将RadioButton添加到RadioGroup之前调用此方法,然后它不应该工作

((RadioGroup) v).check(rbposition);

请检查我的代码是否完美无缺。

public class ThirdActivity extends AppCompatActivity {

        RadioGroup radioGroup;
        ArrayList<String>valueStr=new ArrayList<>();
    LinearLayout ll;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.third_activity);
    ll= (LinearLayout) findViewById(R.id.linearLayout);

            valueStr.add("ONE");
            valueStr.add("TWO");
            valueStr.add("THREE");



            radioGroup=new RadioGroup(ThirdActivity.this);
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {


                   switch (checkedId){
                       case 0:
                           Toast.makeText(ThirdActivity.this,"RadioButton ONE Selected",Toast.LENGTH_LONG).show();
                           break;
                       case 1:
                           Toast.makeText(ThirdActivity.this,"RadioButton TWO Selected",Toast.LENGTH_LONG).show();
                           break;
                       case 2:
                           Toast.makeText(ThirdActivity.this,"RadioButton THREE Selected",Toast.LENGTH_LONG).show();
                           break;
                   }
                }
            });

            int count = 0;
            for (int i=0;i<valueStr.size();i++) {
                RadioButton rb = new RadioButton(ThirdActivity.this);
                rb.setId(count);
                rb.setTag(valueStr.get(i));
                rb.setText(valueStr.get(i));

                rb.setTextSize(16);

                ((RadioGroup) radioGroup).addView(rb);
                if(count==1){
                    radioGroup.check(count);
                }

                count++;
            }
            ll.addView(radioGroup);
        }
    }

答案 2 :(得分:0)

v = new RadioGroup(ctx);
int count = 0;
for (String Str : valueStr.get(Position)) {
    RadioButton rb = new RadioButton(ctx);
    rb.setId(count);
    rb.setTag(valueStrID.get(position).get(count));
    rb.setText(Str);
    rb.setTextSize(fontSize);
    ((RadioGroup) v).addView(rb);
    if (formFieldObject.getFD_ValueField().equals(String.valueOf(Str))) {
        rbposition = count;
    }
    count++;
}

((RadioGroup) v).setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup rg, int arg1) {
        // TODO Auto-generated method stub                                
    }
});

((RadioGroup) v).check(rbposition);

我在check()方法之前给出了监听器,现在它正在运行。