如何保存单选按钮的状态 [英] how to save the state of radio buttons
本文介绍了如何保存单选按钮的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经制作了4个单选按钮,并且想要在单击它们中的任何一个时保存状态,然后想在应用程序中使用该保存的状态.我该怎么做?
I have made 4 radio buttons and want to save the state when any of them is clicked and then want to use that saved state in the application.How can i do that?
myOption1.setChecked(true);
myOption2.setChecked(true);
myOption3.setChecked(true);
myOption4.setChecked(true);
推荐答案
如果总共只有4个单选按钮,则最好将它们的值"存储在 SharedPreferences
中(永久存储).
If you're only going to have 4 radio buttons all in all, you might as well store their "values" in SharedPreferences
(persistant storage).
示例:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor shEditor = sharedPreferences.edit();
shEditor.putBoolean("checkbox_1", myOptionOne.isChecked());
shEditor.putBoolean("checkbox_2", myOptionTwo.isChecked());
shEditor.putBoolean("checkbox_3", myOptionThree.isChecked());
shEditor.putBoolean("checkbox_4", myOptionFour.isChecked());
shEditor.commit();
通过以下操作使用它:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
myOptionOne.setChecked(sharedPreferences.getBoolean("checkbox_1", false));
myOptionTwo.setChecked(sharedPreferences.getBoolean("checkbox_2", false));
myOptionThree.setChecked(sharedPreferences.getBoolean("checkbox_3", false));
myOptionFour.setChecked(sharedPreferences.getBoolean("checkbox_4", false));
这篇关于如何保存单选按钮的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文