【问题标题】:How to save radio button status to saved/shared preferences?如何将单选按钮状态保存到已保存/共享的首选项?
【发布时间】:2016-06-06 16:24:28
【问题描述】:

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

public class PersonalDetailsf extends Activity {

    private SharedPreferences sharedPreferences;  

    private RadioGroup radioGroup;
    private RadioButton radioSexButton;
    private RadioButton rdoMale;

这是我的创作:

    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);

这是在我的 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);

【问题讨论】:

  • 您是如何尝试保存所选状态的?

标签: android radio-button sharedpreferences


【解决方案1】:

onClick()里面做

sharedPreferences.edit().putBoolean("bntChecked", rdoMale.isChecked()).apply();

"btnChecked" 将是您使用 sharedPreferences 获取单选按钮状态的关键

【讨论】:

  • 很高兴您发现它有帮助!
  • 知道如何以类似的方式保存微调器吗?
  • 使用类似的东西,这取决于你要保存什么,是微调器的值还是条目的值。看看这篇文章,了解如何从微调器stackoverflow.com/questions/1947933/how-to-get-spinner-value获取数据
【解决方案2】:

同时创建保存和加载方法:

保存方法

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

加载方法

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

所以要保存您的按钮状态,只需致电saveRadioButtons()

并且要重新加载您的按钮状态,只需在代码中的某处调用loadRadioButtons(),例如您的onCreate()

希望对你有帮助

【讨论】:

  • 只是想知道如何保存和加载单选按钮,因为无法获得最后一部分?例如,我的字符串当前加载如下: strAge = loadSavedPreference("strAge");并按如下方式保存:savePreference("strAge",strAge);
  • 您可以添加它,如果您愿意,这只是一个示例,它的作用是保存单选按钮 checked/unchecked 的状态以及加载它时,它会将单选按钮恢复到上次保存的状态。
  • 没有如如何写“strAge = loadSavedPreference("strAge");”和 "savePreference("strAge",strAge);"与字符串相对的单选按钮的一部分?
  • 我真的很抱歉,但我不知道你的意思/想要什么。你能试着解释一下吗?
  • 要使用这些保存和加载方法,我需要调用它们。对于字符串,我使用 "strAge = loadSavedPreference("strAge");" 这一行保存和 "savePreference("strAge",strAge);"载入。 (您可以在我原来的问题中看到这一点)但是我需要类似的行,但要使用单选按钮。
猜你喜欢
  • 2012-11-29
  • 2012-11-14
  • 2019-12-18
  • 1970-01-01
  • 2023-03-08
  • 2013-09-24
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多