共享首选项中的单选按钮和微调器?
Posted
技术标签:
【中文标题】共享首选项中的单选按钮和微调器?【英文标题】:radiobuttons and spinners in shared preferences? 【发布时间】:2014-05-14 12:31:22 【问题描述】:我在 RadioGroup 中有两个性别单选按钮
1)男性
2)女性
我想将它存储在 sharedpreferences 中,并在我再次打开它时检索它。
除了我想将微调器保存在 sharedpreferences 中,我怎样才能保存它。
【问题讨论】:
【参考方案1】:// Try this way,hope this will help you...
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<RadioGroup
android:id="@+id/rdgGender"
android:layout_
android:layout_>
<RadioButton
android:id="@+id/rdbMale"
android:layout_
android:layout_
android:text="Male"/>
<RadioButton
android:id="@+id/rdbFemale"
android:layout_
android:layout_
android:text="Female"/>
</RadioGroup>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
private RadioGroup rdgGender;
private RadioButton rdbMale;
private RadioButton rdbFemale;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rdgGender = (RadioGroup) findViewById(R.id.rdgGender);
rdbMale = (RadioButton) findViewById(R.id.rdbMale);
rdbFemale = (RadioButton) findViewById(R.id.rdbFemale);
rdgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
switch (checkedId)
case R.id.rdbMale:
editor.putInt("genderValue", R.id.rdbMale);
break;
case R.id.rdbFemale:
editor.putInt("genderValue",R.id.rdbFemale);
break;
editor.commit();
);
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
switch (sharedPreferences.getInt("genderValue",R.id.rdbMale))
case R.id.rdbMale:
rdbMale.setChecked(true);
break;
case R.id.rdbFemale:
rdbFemale.setChecked(true);
break;
【讨论】:
我这里有点问题。当我设置女性。重新运行应用程序后。收音机默认为男性。以上是关于共享首选项中的单选按钮和微调器?的主要内容,如果未能解决你的问题,请参考以下文章