从另一个活动中的共享偏好中检索放射性组选择?

时间:2016-07-29 18:16:45

标签: android

我看过并试过不同的方法,所以我肯定做错了。

我在"磅"的主要活动中有一个放射性组选择。或"公斤"。无论他们选择哪种无线电,都应该在下一次活动中更改为该文本。

Lbs是当前的默认广播,并且被检查为" true"。

我在第二个活动中放了一个按钮,返回到收音机选择屏幕。但是,在选择" kgs"之后,它会继续显示" lbs"。

主要活动

@Override
public void onResume() {
    super.onResume();

    SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
    String enteredWeightState = sharedPreferences.getString("entered_weight", "");
    int lbsState = sharedPreferences.getInt("UOM_checkId", UOM_RadioGroup.getCheckedRadioButtonId());

    UOM_RadioGroup.check(lbsState);
    enter_weight_editText.setText(enteredWeightState);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    UOM_RadioGroup = (RadioGroup) findViewById(R.id.UOM_RadioGroup);
    enter_weight_editText = (EditText)findViewById(R.id.enter_weight_editText);

    UOM_RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        public void onCheckedChanged(RadioGroup UOM_RadioGroup, int checkedId) {

            if (checkedId == R.id.UOM_pounds) {

                SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("UOM_checkId", checkedId);
                editor.apply();

            } else {

                SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("UOM_checkId", checkedId);
                editor.apply();
            }

        }

    });
}


    public void beginRoutineButton(View view) {

        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("entered_weight", enter_weight_editText.getText().toString());
        editor.apply();

        Intent intent = new Intent(this, Activity2.class);
        startActivity(intent);
    }

}



Activity2

    public TextView unit_of_measure;
    public View fragment1;
    public Button backButton;
    public TextView weight;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);

        weight = (TextView) findViewById(R.id.weight);
        backButton = (Button) findViewById(R.id.backButton);
        unit_of_measure = (TextView) findViewById(R.id.unit_of_measure);

        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);

        int lbsState = sharedPreferences.getInt("UOM_checkId", -1);
        String enteredWeightState = sharedPreferences.getString("entered_weight", "");


        weight.setText(enteredWeightState);


        if (lbsState < 0)
            unit_of_measure.setText("kgs");
        else
            unit_of_measure.setText("lbs");


    }


    public void backButton(View view) {

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}

1 个答案:

答案 0 :(得分:0)

创建一个名为SharedPreferences的类,并在其中创建一个方法init并初始化首选项,如下所示: -

public class SharedPreferences{
    private static Context mContext;
    private static SharedPreferences preferences = null;
    private static SharedPreferences.Editor editor;
    public static void init(Context mContext) {
        SharedPreferences.mContext = Env.appContext;
        SharedPreferences.preferences = mContext.getSharedPreferences("user_info", Context.MODE_PRIVATE);
        SharedPreferences.editor = preferences.edit();
    }
}

并将启动器活动中的SharedPreferences的init方法调用为SharedPreferences.init(),并在下面的首选项中设置getter setter: -

public static void setWeight(String weight){
editor.putString("entered_weight", weight).commit();
}

public static String getWeight(){
 return preferences.getString("entered_weight", null);
}

当您想设置权重调用SharedPreferences.setWeight(ed.getText.toString());并使用SharedPreferences.getWeight();获取价值时。您可以从应用程序内部的任何位置获取值,您唯一需要做的就是在启动器活动上调用init()方法并使用保存值并在sharedPreferences中获取值。

如上所述它会帮助你。