如何设置单选按钮的默认选择单选按钮

时间:2018-12-12 02:43:58

标签: java android android-layout android-radiobutton android-radiogroup

我创建了一个动态单选组,其中包含单击时每个单选按钮的onClickListener。如何将单选组中的第一项设置为默认选中项,包括第一项的onClick操作。

我尝试使用代码((RadioButton)radioGroup.getChildAt(0)).setChecked(true);,但它仅将项目设置为选中状态,但未执行任何onClick操作。

以下是我为动态单选按钮所做的操作。

public static void createRoutemapButton(final Activity activity, final List<DORouteMapInfo> list, final FlexRadioGroup flexRadioGroup, final ImageView imageView){
    LayoutInflater inflater = LayoutInflater.from(activity);

    float margin = DensityUtils.dp2px(activity, 85);
    float width = DensityUtils.getWidth(activity);

    for (final DORouteMapInfo routeMap : list) {
        final RadioButton rb = (RadioButton) inflater.inflate(R.layout.item_radiobutton_label, null);
        rb.setText(routeMap.getTitle());
        FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams((int) (width - margin) / 2, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(5,0,5,5);
        rb.setLayoutParams(params);
        rb.setPadding(0,10,0,10);
        flexRadioGroup.addView(rb);

        flexRadioGroup.setOnCheckedChangeListener(new FlexRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(@IdRes int checkedId) {
            }
        });
        rb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Picasso.with(activity)
                        .load(routeMap.getImageURL())
                        .fit().centerInside()
                        .into(imageView);
            }
        });
    }
} 

0 个答案:

没有答案