Android 如何使用RadioGroup创建设置面板并保存更改

Android 如何使用RadioGroup创建设置面板并保存更改,android,radio-button,settings,background-color,radio-group,Android,Radio Button,Settings,Background Color,Radio Group,在我的Android应用程序中,有两个活动(Main和Settings),分别是Main.xml和Settings.xml。我想在设置中创建一个RadioGroup,它的工作方式类似于用户的设置面板,以便用户可以选择按钮的背景颜色。我正在尝试对每个单选按钮使用onClick,它工作得很好,但如果我返回main.xml,则不会保存che color。如何为其他活动中按钮的设置背景色创建设置面板并保存它(或使用用户保存按钮) 此处为设置代码: public class Settings extend

在我的Android应用程序中,有两个活动(Main和Settings),分别是Main.xml和Settings.xml。我想在设置中创建一个RadioGroup,它的工作方式类似于用户的设置面板,以便用户可以选择按钮的背景颜色。我正在尝试对每个单选按钮使用onClick,它工作得很好,但如果我返回main.xml,则不会保存che color。如何为其他活动中按钮的设置背景色创建设置面板并保存它(或使用用户保存按钮)

此处为设置代码:

public class Settings extends Main implements OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);
final Button back;
back= (Button) findViewById(R.id.back);

RadioButton redbtn, blubtn, grebtn;
redbtn= (RadioButton) findViewById(R.id.redbtn);
blubtn= (RadioButton) findViewById(R.id.blubtn);
grebtn= (RadioButton) findViewById(R.id.grebtn);

redbtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        back.setBackgroundColor(Color.RED);
        add.setBackgroundColor(Color.RED);
    }
});
back.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        finish();
    }
});
}

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
    // TODO Auto-generated method stub
}   
}
settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/title_settings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="@string/settings"
    android:textSize="40sp" />
<TextView
    android:id="@+id/ButtonSettingsView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/title_settings"
    android:layout_marginTop="17dp"
    android:text="@string/BtnSet"
    android:textSize="30sp" />

<Button
    android:id="@+id/back"
    android:layout_width="70sp"
    android:layout_height="70sp"
    android:layout_alignBaseline="@+id/title_settings"
    android:layout_alignBottom="@+id/title_settings"
    android:layout_alignRight="@+id/ButtonSettingsView"
    android:text="Back"/>

<RadioGroup
    android:id="@+id/ButtonSettings"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ButtonSettingsView"
    android:layout_marginTop="14dp"
    android:orientation="vertical" >
    <RadioButton
        android:id="@+id/redbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/red"/>
    <RadioButton
        android:id="@+id/blubtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/blue"  />
    <RadioButton
        android:id="@+id/grebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/grey"/>
</RadioGroup>
</RelativeLayout>


谢谢

当然,它不会将其保存到其他
活动中
,因为当您启动另一个
活动时,该
活动将使用xml中的默认值。您需要以某种方式保存颜色,然后在其他
活动中引用它。如果你想让它在用户离开并重新进入应用程序时保持不变,一种方法就是使用。然后,您可以在其他
活动中打开这些pref,保存值,并相应地更改背景

如果您只希望它在用户当前在应用程序中时更改颜色,那么您可以创建一个
静态
类来存储此变量,并根据该值更改颜色