Android 如何将微调器selecteditem保存到SharedReferences?

Android 如何将微调器selecteditem保存到SharedReferences?,android,Android,我已经知道如何将edittext内容保存到SharedReferences,但在微调器和RadioGroup中,我仍然没有任何线索。你能给我一些代码片段吗?感谢,因为数据存储与用于显示或修改值的UI元素无关。以下是如何存储或检索各种数据类型的说明: 因此,微调器选择只是一个整数(或字符串,如果您愿意),而无线组的选择只是您选择的表示该选择的任何标识符(如字符串)。 如果选择来自数组资源,则可以使用数组中的值或数组中的索引。在共享首选项中存储/检索它们,就像您用于存储和检索EditText中的文本

我已经知道如何将edittext内容保存到SharedReferences,但在微调器和RadioGroup中,我仍然没有任何线索。你能给我一些代码片段吗?感谢

,因为数据存储与用于显示或修改值的UI元素无关。以下是如何存储或检索各种数据类型的说明:

因此,微调器选择只是一个整数(或字符串,如果您愿意),而无线组的选择只是您选择的表示该选择的任何标识符(如字符串)。
如果选择来自数组资源,则可以使用数组中的值或数组中的索引。在共享首选项中存储/检索它们,就像您用于存储和检索EditText中的文本一样。

对于数据存储,与使用哪些UI元素显示或修改值无关。以下是如何存储或检索各种数据类型的说明:

因此,微调器选择只是一个整数(或字符串,如果您愿意),而无线组的选择只是您选择的表示该选择的任何标识符(如字符串)。
如果选择来自数组资源,则可以使用数组中的值或数组中的索引。在共享首选项中存储/检索它们,就像在编辑文本中存储和检索文本一样。

这是将微调器的选定项保存在共享引用中的方法:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
       Object obj = parent.getItemAtPosition(pos);
       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
       Editor prefsEditor = prefs.edit();
       prefsEditor.putString("object", obj.toString());
       prefsEditor.commit();            
    }
    public void onNothingSelected(AdapterView<?> parent) { }
});
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
已选择公共位置(AdapterView父项、视图、整数位置、长id){
Object obj=parent.getItemAtPosition(pos);
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(this.getApplicationContext());
编辑器prefsEditor=prefs.edit();
putString(“object”,obj.toString());
提交();
}
未选择的公共无效(AdapterView父项){}
});

这是在SharedReferences中保存微调器选定项的方法:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
       Object obj = parent.getItemAtPosition(pos);
       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
       Editor prefsEditor = prefs.edit();
       prefsEditor.putString("object", obj.toString());
       prefsEditor.commit();            
    }
    public void onNothingSelected(AdapterView<?> parent) { }
});
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
已选择公共位置(AdapterView父项、视图、整数位置、长id){
Object obj=parent.getItemAtPosition(pos);
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(this.getApplicationContext());
编辑器prefsEditor=prefs.edit();
putString(“object”,obj.toString());
提交();
}
未选择的公共无效(AdapterView父项){}
});

很高兴它对您有帮助。很高兴它对您有帮助。