我正在使用自定义单选按钮。它工作正常(因为它在options.xml中定义)但是当我从options.xml切换到main.xml时,它将变为默认值,意味着它不再突出显示。它应该工作,直到我按它它不应该转为默认..这里是radiobutton_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radio_down" android:state_checked="true"/>
<item android:drawable="@drawable/radio" android:state_checked="false"/>
</selector>
我在options.xml中使用这些来调用单选按钮设置。
<RadioGroup
android:id="@+id/sound"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_gravity="right"
android:layout_marginRight="0dp"
android:layout_marginTop="50dp"
android:orientation="vertical"
android:padding="0dp" >
<RadioButton
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:button="@drawable/radiobutton_selector"
android:id="@+id/on"
/>
<RadioButton
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:button="@drawable/radiobutton_selector"
android:id="@+id/off"
/>
</RadioGroup>
请帮我弄清问题。在此先感谢!!!!!
答案 0 :(得分:2)
您可以为RadioGroup
执行此操作。
你需要保存你选择的单选按钮,因为你可以使用下面的一个变量。
int check_radio=-1;
public static final int RADIO_BUTTON_ON=1;
public static final int RADIO_BUTTON_OFF=2;
mRadioGroup
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
switch(checkedId){
case R.id.on:
//Radio Button on is True
check_radio=RADIO_BUTTON_ON;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("RadioButton", RADIO_BUTTON_ON);
// Commit the edits!
editor.commit();
break;
case R.id.off:
//Radio Button off is True
check_radio=RADIO_BUTTON_OFF;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("RadioButton", RADIO_BUTTON_OFF);
// Commit the edits!
editor.commit();
break;
}
});
现在,您的活动恢复,您可以检查下面的一个条件。 从SharedPrefrence获取价值如下代码;
//If you have save your value in SharedPrefrence it will return your stored int value.
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
check_radio = settings.getInt("RadioButton", -1);
if(check_radio==RADIO_BUTTON_ON){
mRadioOn.setChecked(true);
}else if(check_radio==RADIO_BUTTON_OFF){
mRadioOff.setChecked(true);
}
您可以通过以下步骤使用SharedPreferences
public static final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);