无线电组未正确保存

发布于 2024-12-29 06:52:05 字数 1312 浏览 0 评论 0原文

经过多次尝试和错误后,我决定问这个问题。我无法保存我的单选按钮组 ID,因此当我返回到使用单选按钮的活动时,所有按钮再次变为空。

这是我的代码:

public class Random extends Activity implements RadioGroup.OnCheckedChangeListener, OnClickListener {

... ...

这就是我提交选中的单选按钮 id 的方式

public void onCheckedChanged (RadioGroup group, int checkedId) {

        int profileGroup_CheckId = profileGroup.getCheckedRadioButtonId();
        int configGroup_CheckId = configGroup.getCheckedRadioButtonId();

        SharedPreferences profileGroupPrefs = getSharedPreferences("profileGroupPrefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = profileGroupPrefs.edit();
        prefsEditor.putInt("profileGroup_CheckId", profileGroup_CheckId);
        prefsEditor.putInt("configGroup_CheckId", configGroup_CheckId);
        prefsEditor.commit();
}

,并且我已将其添加到 onCreate

public void onCreate(Bundle savedInstanceState) {
    SharedPreferences profileGroupPrefs = this.getSharedPreferences("profileGroupPrefs", MODE_WORLD_READABLE);
    profileGroupPrefs.getInt("profileGroup_CheckId", 0);
    profileGroupPrefs.getInt("configGroup_CheckId", 0);

Now 中,因为值保存在我的应用程序的共享首选项中,因为我已检查了 profileGroup.xml,但单选按钮未选中一旦我回来。

我尝试了用户在这里列出的许多其他方法,但没有一个有效。一些帮助将不胜感激。

After a lot of trial and error i decided to ask this question. I cannot save my radio group id, so when i return to the activity which uses radio buttons, all are empty once again.

Here is my code:

public class Random extends Activity implements RadioGroup.OnCheckedChangeListener, OnClickListener {

...
...

This is how i commit the checked radio button id

public void onCheckedChanged (RadioGroup group, int checkedId) {

        int profileGroup_CheckId = profileGroup.getCheckedRadioButtonId();
        int configGroup_CheckId = configGroup.getCheckedRadioButtonId();

        SharedPreferences profileGroupPrefs = getSharedPreferences("profileGroupPrefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = profileGroupPrefs.edit();
        prefsEditor.putInt("profileGroup_CheckId", profileGroup_CheckId);
        prefsEditor.putInt("configGroup_CheckId", configGroup_CheckId);
        prefsEditor.commit();
}

and I have added this in onCreate

public void onCreate(Bundle savedInstanceState) {
    SharedPreferences profileGroupPrefs = this.getSharedPreferences("profileGroupPrefs", MODE_WORLD_READABLE);
    profileGroupPrefs.getInt("profileGroup_CheckId", 0);
    profileGroupPrefs.getInt("configGroup_CheckId", 0);

Now, from the values ARE being saved inside shared_prefs of my app, since i have checked the profileGroup.xml, but the Radio Buttons are not checked once i return.

I have tried many other ways listed by users here, but none have worked. Some help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2025-01-05 06:52:05

试试这个。

public void onCheckedChanged (RadioGroup group, int checkedId) {    
    int profileGroup_CheckId = profileGroup.getCheckedRadioButtonId();
    int configGroup_CheckId = configGroup.getCheckedRadioButtonId();

    SharedPreferences profileGroupPrefs = getSharedPreferences("profileGroupPrefs", MODE_WORLD_WRITABLE);
    SharedPreferences.Editor prefsEditor = profileGroupPrefs.edit();
    prefsEditor.putInt("profileGroup_CheckId", profileGroup_CheckId);
    prefsEditor.putInt("configGroup_CheckId", configGroup_CheckId);
    prefsEditor.commit();
)

public void onResume() {
super.onResume();
SharedPreferences profileGroupPrefs = this.getSharedPreferences("profileGroupPrefs", MODE_WORLD_READABLE);
    int a=  profileGroupPrefs.getInt("profileGroup_CheckId", 0);
    int b=  profileGroupPrefs.getInt("configGroup_CheckId", 0);
     profileGroup.check(a); 
     configGroup.check(b);
    }

try this one.

public void onCheckedChanged (RadioGroup group, int checkedId) {    
    int profileGroup_CheckId = profileGroup.getCheckedRadioButtonId();
    int configGroup_CheckId = configGroup.getCheckedRadioButtonId();

    SharedPreferences profileGroupPrefs = getSharedPreferences("profileGroupPrefs", MODE_WORLD_WRITABLE);
    SharedPreferences.Editor prefsEditor = profileGroupPrefs.edit();
    prefsEditor.putInt("profileGroup_CheckId", profileGroup_CheckId);
    prefsEditor.putInt("configGroup_CheckId", configGroup_CheckId);
    prefsEditor.commit();
)

public void onResume() {
super.onResume();
SharedPreferences profileGroupPrefs = this.getSharedPreferences("profileGroupPrefs", MODE_WORLD_READABLE);
    int a=  profileGroupPrefs.getInt("profileGroup_CheckId", 0);
    int b=  profileGroupPrefs.getInt("configGroup_CheckId", 0);
     profileGroup.check(a); 
     configGroup.check(b);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文