简体   繁体   English

如何保存在方向更改时选中的单选按钮?

[英]How to save radio button checked on orientation change?

I have an Activity with a FrameLayout to load fragment and a RadioGroup with four radio button.我有一个带有 FrameLayout 的 Activity 来加载片段和一个带有四个单选按钮的 RadioGroup。 Let's say RadioButton rb1, rb2, rb3, rb4 will load different Fragment F1, F2, F3, F4 respectively on the checkChanged and default it will load F1 fragment.假设 RadioButton rb1、rb2、rb3、rb4 将分别在 checkChanged 上加载不同的 Fragment F1、F2、F3、F4,默认情况下它将加载 F1 片段。

Now, if i am selecting rb2 then it is loading fragment F2 and after orientation change again it is loading F1.现在,如果我选择 rb2 那么它正在加载片段 F2 并且在再次改变方向后它正在加载 F1。 Bellow is my code, please give any suggestion how to handle it.贝娄是我的代码,请提供任何建议如何处理它。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRadioWidget = (RadioWidget) findViewById(R.id.segmented_control);
        mRadioWidget.mRGroup.setOnCheckedChangeListener(this);

        if (savedInstanceState == null) {
            HomeFragment homeFragment = new HomeFragment();
            loadFragment(false, homeFragment, HomeFragment.CLASSTAG);
        }
    }

@Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {

        case R.id.rb_home:

            HomeFragment homeFragment = new HomeFragment();
            loadFragment(false, homeFragment, HomeFragment.CLASSTAG);
            break;

        case R.id.rb_config:

            ConfigFragment configFragment = new ConfigFragment();
            loadFragment(false, configFragment, ConfigFragment.CLASSTAG);
            break;

        case R.id.rb_flash:

            Toast.makeText(getApplicationContext(), "Not yet implemented", Toast.LENGTH_LONG).show();
            break;

        case R.id.rb_fav:

            FavoritesFragment favFragment = new FavoritesFragment();
            loadFragment(false, favFragment, FavoritesFragment.CLASSTAG);
            break;
        }
    }

You should try to remember the fragment that was last loaded by putting some sort of a value in the savedInstanceState Bundle.您应该尝试通过在savedInstanceState Bundle 中放置某种值来记住上次加载的片段。

This is how you can save and retrieve data : Saving Android Activity state using Save Instance State这是保存和检索数据的方法: 使用 Save Instance State 保存 Android Activity 状态

When in onCreate the bundle is not null, retrieve that value from the bundle, and then load the correct fragment and update the checkbox.onCreate包不为空时,从包中检索该值,然后加载正确的片段并更新复选框。

在 AndroidManifest 文件中,在您的活动中添加android:configChanges="orientation|screenSize"

If you set id s attributes to <RadioButton> views in you .mxl file, the system will save the state for you, same as happens with <EditText> views.如果您在 .mxl 文件中将id的属性设置为<RadioButton>视图,系统将为您保存状态,与<EditText>视图相同。 It doesn't work if you only set the id for <RadioGroup> .如果您只为<RadioGroup>设置id ,则它不起作用。

For example例如

Before (bad)之前(坏)

    <RadioButton
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       ... />

Now现在

    <RadioButton
       android:id="@+id/radio_example" # <----- missing property
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       ... />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式更改单选按钮的选中颜色 - How to change checked color of Radio Button programmatically 如何在 android 工作室中保存选中的单选按钮的 state - How to save state of checked radio button in android studio 以编程方式更改onOptionsItemSelected内部选中的单选按钮 - Change programatically a radio button checked inside onOptionsItemSelected Android-如果选中了单选按钮,如何启用按钮? - Android - How to enable a button if a radio button is checked? 如何使用带有共享首选项的 android studio 在测验应用程序中保存单选按钮的检查状态 - How to save Checked Status of radio button in Quiz app using android studio with sharedpreferences 如何使用共享首选项保存在微调器上和从选中的单选按钮中选择的数据 - How to use Shared Preferences to save data selected on Spinner and from checked Radio Button 如何保持单选按钮处于选中状态,直到在Android中的单选组中选中了另一个单选按钮? - How to keep the radio button checked till its other radio button is checked in a radio group in android? 如果选中单选按钮,如何检查复选框 - How to check checkbox if radio button is checked 如何在radiogroup中将单选按钮设置为默认值? - How to set radio button checked as default in radiogroup? 如何在自定义对话框上获取选中的单选按钮? - How to get the checked radio button on a custom dialog?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM