如何将单选按钮状态保存到已保存/共享首选项?

Coc*_*o12 3 android radio-button sharedpreferences

我可以在保存的首选项中保存字符串,但无法保存单选按钮。

public class PersonalDetailsf extends Activity {

    private SharedPreferences sharedPreferences;  

    private RadioGroup radioGroup;
    private RadioButton radioSexButton;
    private RadioButton rdoMale;
Run Code Online (Sandbox Code Playgroud)

这是我的创建:

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    String strAge = Integer.toString(age);
    String strHeight = Integer.toString(height);
    String strWeight = Integer.toString(weight);

    name = loadSavedPreference("name");
    strAge = loadSavedPreference("strAge");
    strHeight = loadSavedPreference("strHeight");
    strWeight = loadSavedPreference("strWeight");

    etName.setText(name);
    etAge.setText(strAge);
    etHeight.setText(strHeight);
    etWeight.setText(strWeight);
Run Code Online (Sandbox Code Playgroud)

这是在按钮后面的 onCLick 中,我在其中使用单选按钮并保存字符串:

            Name = etName.getText().toString();
            age = (int) Double.parseDouble(etAge.getText().toString());
            height = (int) Double.parseDouble(etHeight.getText().toString());
            weight = (int) Double.parseDouble(etWeight.getText().toString());


            int selectedId = radioGroup.getCheckedRadioButtonId();

            radioSexButton = (RadioButton) findViewById(selectedId);
            rdoMale = (RadioButton) findViewById(R.id.rdoMale);

            if(rdoMale.isChecked())
            {
                BMR = 10 * weight + 6.25 * height - 5 * age + 5;

            }
                else
            {
                    BMR = 10 * weight + 6.25 * height - 5 * age -161;

            }


            //Save Preferences
            String strAge = Integer.toString(age);
            String strHeight = Integer.toString(height);
            String strWeight = Integer.toString(weight);

            name = etName.getText().toString();
            savePreference("name",name);

            strAge = etAge.getText().toString();
            savePreference("strAge",strAge);

            strHeight = etHeight.getText().toString();
            savePreference("strHeight",strHeight);

            strWeight = etWeight.getText().toString();
            savePreference("strWeight",strWeight);
Run Code Online (Sandbox Code Playgroud)

Str*_*der 5

创建保存和加载方法:

保存方法

public void saveRadioButtons(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("Gender", radioSexButton.isChecked());
    editor.putBoolean("Male", rdoMale.isChecked());
    editor.apply();
}
Run Code Online (Sandbox Code Playgroud)

负载方式

public void loadRadioButtons(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    radioSexButton.setChecked(sharedPreferences.getBoolean("Gender", false)); 
    rdoMale.setChecked(sharedPreferences.getBoolean("Male", false));
}
Run Code Online (Sandbox Code Playgroud)

因此,要保存按钮状态,只需调用saveRadioButtons()

加载按钮状态,只需调用loadRadioButtons()代码中的某个位置,例如onCreate()

希望这能帮助你