You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

关于Android SharedPreference的疑问:保存单选组状态控制后续界面

嘿,我来帮你搞定这个需求!要保存两个RadioGroup的选中状态,再用这些状态控制下一个Activity的样式,其实分几步就能实现,我给你一步步拆解:

第一步:保存并恢复RadioGroup的选中状态

你已经用到了SharedPreferences,但最好在选中状态变化时就实时保存,而不是只在onCreate里处理。另外,Activity重启时还要记得恢复之前的选中状态,这样用户体验更好。

完整的代码示例如下:

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

    // 初始化两个RadioGroup
    RadioGroup tapRadioGroup = findViewById(R.id.tap_radio_group);
    RadioGroup anotherRadioGroup = findViewById(R.id.another_radio_group);

    // 获取SharedPreferences实例
    SharedPreferences radioButtonPref = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = radioButtonPref.edit();

    // 为第一个RadioGroup设置选中监听,实时保存状态
    tapRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
        editor.putInt(getString(R.string.tap_checked), checkedId);
        editor.apply(); // 异步保存,比commit()更高效,适合大多数场景
    });

    // 第二个RadioGroup同理
    anotherRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
        editor.putInt(getString(R.string.another_checked), checkedId);
        editor.apply();
    });

    // 恢复之前保存的选中状态
    int savedTapChecked = radioButtonPref.getInt(getString(R.string.tap_checked), -1);
    if (savedTapChecked != -1) {
        tapRadioGroup.check(savedTapChecked);
    }

    int savedAnotherChecked = radioButtonPref.getInt(getString(R.string.another_checked), -1);
    if (savedAnotherChecked != -1) {
        anotherRadioGroup.check(savedAnotherChecked);
    }
}

注意:要在strings.xml里定义对应的存储key,比如:

<string name="tap_checked">tap_radio_selected_id</string>
<string name="another_checked">another_radio_selected_id</string>
第二步:把选中状态传递给下一个Activity

当你要跳转到下一个Activity时,有两种方式传递状态:

  1. 从SharedPreferences读取后,通过Intent传递
  2. 直接在目标Activity里读取SharedPreferences

推荐第一种方式(更灵活),代码示例:

// 假设你通过按钮触发跳转
Button goNextBtn = findViewById(R.id.go_next_btn);
goNextBtn.setOnClickListener(v -> {
    Intent intent = new Intent(MainActivity.this, NextActivity.class);
    
    // 读取保存的选中状态
    SharedPreferences radioPref = PreferenceManager.getDefaultSharedPreferences(this);
    int tapCheckedId = radioPref.getInt(getString(R.string.tap_checked), -1);
    int anotherCheckedId = radioPref.getInt(getString(R.string.another_checked), -1);
    
    // 把状态放入Intent
    intent.putExtra("TAP_SELECTED_ID", tapCheckedId);
    intent.putExtra("ANOTHER_SELECTED_ID", anotherCheckedId);
    
    startActivity(intent);
});
第三步:在目标Activity中根据状态设置显示样式

在NextActivity里,读取传递过来的状态,然后根据不同的选中ID来调整UI样式(比如背景色、布局可见性、控件样式等):

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

    // 获取传递过来的状态
    Intent intent = getIntent();
    int tapCheckedId = intent.getIntExtra("TAP_SELECTED_ID", -1);
    int anotherCheckedId = intent.getIntExtra("ANOTHER_SELECTED_ID", -1);

    // 根据第一个RadioGroup的选中状态设置样式
    if (tapCheckedId == R.id.radio_option1) {
        // 比如设置页面背景为黄色
        getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.yellow));
    } else if (tapCheckedId == R.id.radio_option2) {
        // 设置页面背景为蓝色
        getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.blue));
    }

    // 根据第二个RadioGroup的选中状态调整布局
    if (anotherCheckedId == R.id.radio_style_a) {
        findViewById(R.id.style_a_layout).setVisibility(View.VISIBLE);
        findViewById(R.id.style_b_layout).setVisibility(View.GONE);
    } else if (anotherCheckedId == R.id.radio_style_b) {
        findViewById(R.id.style_b_layout).setVisibility(View.VISIBLE);
        findViewById(R.id.style_a_layout).setVisibility(View.GONE);
    }
}
一些小提示
  • 如果你的RadioGroup选项是动态生成的,要确保保存的ID在下次启动时仍然有效,避免出现找不到控件的情况。
  • apply()是异步保存数据,不会阻塞主线程;commit()是同步保存,适合必须确保数据立即写入的场景(比如退出App前)。

内容的提问来源于stack exchange,提问作者duy anh hoang

火山引擎 最新活动