无线电组未正确保存
经过多次尝试和错误后,我决定问这个问题。我无法保存我的单选按钮组 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个。
try this one.