如何在应用启动时将数据从SharedPreferences还原到TextView?

时间:2016-02-04 19:31:43

标签: android sharedpreferences

我的应用从SharedPreferences中的TextView保存文本(类型字符串)。然后在其他五个TextView中显示来自SharedPreferences的最后5个数字。它的工作非常好。

有一个问题。如何在应用启动时将数据从SharedPreferences恢复到所有五个TextView?

公共类MainActivity扩展了AppCompatActivity {

private Button btnSave;
private Button btnCallCalc;
private TextView txtNowDigit;
private TextView firstDigit;
private TextView secondDigit;
private TextView thirdDigit;
private TextView fourthDigit;
private TextView fifthDigit;

private static final int REQUEST_CODE_CALL_CALC = 100;
public static final int RESULT_OK = 111;
public static final String DIGIT_EXTRA = "DIGIT_EXTRA";
public static final String KEY_FIRST_DIGIT = "KEY_FIRST_DIGIT";
public static final String KEY_SECOND_DIGIT = "KEY_SECOND_DIGIT";
public static final String KEY_THIRD_DIGIT = "KEY_THIRD_DIGIT";
public static final String KEY_FOURTH_DIGIT = "KEY_FOURTH_DIGIT";
public static final String KEY_FIFTH_DIGIT = "KEY_FIFTH_DIGIT";

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

    btnSave = (Button) findViewById(R.id.save_btn);
    btnCallCalc = (Button) findViewById(R.id.call_calc);
    txtNowDigit = (TextView) findViewById(R.id.now_result);
    firstDigit = (TextView) findViewById(R.id.first_digit);
    secondDigit = (TextView) findViewById(R.id.second_digit);
    thirdDigit = (TextView) findViewById(R.id.third_digit);
    fourthDigit = (TextView) findViewById(R.id.fourth_digit);
    fifthDigit = (TextView) findViewById(R.id.fifth_digit);

    btnCallCalc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, CalculatorActivity.class);
            startActivityForResult(intent, REQUEST_CODE_CALL_CALC);
        }
    });


    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(KEY_FIFTH_DIGIT, sp.getString(KEY_FOURTH_DIGIT, null));
            editor.putString(KEY_FOURTH_DIGIT, sp.getString(KEY_THIRD_DIGIT, null));
            editor.putString(KEY_THIRD_DIGIT, sp.getString(KEY_SECOND_DIGIT, null));
            editor.putString(KEY_SECOND_DIGIT, sp.getString(KEY_FIRST_DIGIT, null));
            editor.putString(KEY_FIRST_DIGIT, txtNowDigit.getText().toString());
            editor.apply();

            setSavedDigits();
        }
    });
}

private void setSavedDigits() {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

    fifthDigit.setText(sp.getString(KEY_FIRST_DIGIT, null));
    fourthDigit.setText(sp.getString(KEY_SECOND_DIGIT, null));
    thirdDigit.setText(sp.getString(KEY_THIRD_DIGIT, null));
    secondDigit.setText(sp.getString(KEY_FOURTH_DIGIT, null));
    firstDigit.setText(sp.getString(KEY_FIFTH_DIGIT, null));
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) {
        super.onActivityResult(requestCode, resultCode, data);
        return;
    }

    switch (requestCode) {
        case REQUEST_CODE_CALL_CALC:
            if (data == null) {
                return;
            }
            String digit = data.getStringExtra(DIGIT_EXTRA);
            txtNowDigit.setText(String.valueOf(digit));
            break;

        default:
            super.onActivityResult(requestCode, resultCode, data);
    }
}

1 个答案:

答案 0 :(得分:1)

之后致电setSavedDigits()
fifthDigit = (TextView) findViewById(R.id.fifth_digit);

onCreate方法中。