大家好,嘿!
我正在制作一个屏幕底部具有RadioGroup(自定义)的应用程序。当初次进入该应用程序时,它将向您显示如下欢迎片段:(对不起,我无法上传pictureT ^ T,尽管我知道我可以在这里发布图片链接⊙﹏⊙)

------------------------------------| || || || || || || || welcome || || || || || || || || || ||---------------------------------- || ||⊙tab0 ⊙tab1 ⊙tab2 |-------------------------------------

并且未选中RadioButton。

当我触摸tab0时,它将变为另一个片段,如下所示:

------------------------------------|<back || || || || || || || tab0 || content || || || || || || || || ||---------------------------------- || ||*⊙tab0* ⊙tab1 ⊙tab2 |-------------------------------------

似乎正常。但是当按下后退按钮时,它不起作用,当我再次按下时,它起作用了。真是太奇怪了。
这是我的WelcomeFragment代码:

public class WelcomeFragment extends Fragment {

private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.welcome_fragment_layout, null);
    MainActivity.radioGroup.setBackgroundResource(R.drawable.tabbar_bg);
    RadioButton button;
    // Reset the RadioGroup state to not checked。
    for(int i = 0;i<3;i++){
        button = (RadioButton)MainActivity.radioGroup.getChildAt(i);
        //button.setChecked(false);//if I comment this,it work finely!!,but I don't know why
    }
    return view;
}


这是Activity中单选按钮的事件:

    private void setupRadioGroup() {
    resetRadioButtonID();
    radioButton = (RadioButton) radioGroup.getChildAt(0);
    radioButton.setChecked(false);
    radioGroup
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if (checkedId != 1) {
                        radioButton.setChecked(false);
                    }
                    FragmentTransaction transaction = fragmentManager
                            .beginTransaction();
                    Fragment fragment = FragmentFactory
                            .getInstanceByIndex(checkedId);
                    transaction.replace(R.id.fl_content, fragment);
                    transaction.commitAllowingStateLoss();


                    switch (checkedId) {
                    case 1:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar1);
                        break;
                    case 2:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar2);
                        break;
                    case 3:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar3);
                        break;
                    default:
                        break;
                    }
                }
            });
}


顺便说一句,我将非常感激,别管我的英语不好。^-^

最佳答案

您需要将所有内容包装在if条件中的onCheckedChanged内部。
首先检查单选按钮的当前状态。

radioGroup
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(!radioButton.checked) return;  // add this
                    if (checkedId != 1) {
                        radioButton.setChecked(false);
                    }
                    FragmentTransaction transaction = fragmentManager
                            .beginTransaction();
                    Fragment fragment = FragmentFactory
                            .getInstanceByIndex(checkedId);
                    transaction.replace(R.id.fl_content, fragment);
                    transaction.commitAllowingStateLoss();


                    switch (checkedId) {
                    case 1:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar1);
                        break;
                    case 2:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar2);
                        break;
                    case 3:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar3);
                        break;
                    default:
                        break;
                    }
                }
            });

08-06 04:35