如何修复我的 onSaveInstanceState 和 onRestoreInstanceState?
How to fix my onSaveInstanceState and onRestoreInstanceState?
我试图保存屏幕旋转期间用户的所有值和点击,但我的按钮点击和文本视图不起作用。我花了很多时间试图弄清楚在这个方法中写什么,none 成功了。来自 Select 您的提示按钮和 textView 输出的输入目前未被保存。
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState: Started");
String text = txtNumOfSplits.getText().toString();
int no = Integer.parseInt(text);
outState.putInt("count", no);
String num_of_splits = txtNumOfSplits.getText().toString();
//Convert String to Integer
int no2 = Integer.parseInt(num_of_splits);
outState.putInt("count", no2);
money = Double.parseDouble(edtBillAmount.getText().toString());
outState.putString("bill", String.valueOf(money));
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "onRestoreInstanceState: Started");
String text = txtNumOfSplits.getText().toString();
int no = savedInstanceState.getInt("count");
txtNumOfSplits.setText(no + "");
money = Double.parseDouble(edtBillAmount.getText().toString());
String money = savedInstanceState.getString("bill");
txtTotalBill.setText("Total Bill\n$" + finalAmount.toString());
}
btn_add = findViewById(R.id.btn_add);
btn_subtract = findViewById(R.id.btn_subtract);
btn_20 = findViewById(R.id.btn_20);
btn_15 = findViewById(R.id.btn_15);
btn_10 =findViewById(R.id.btn_10);
btn_calculate = findViewById(R.id.btn_calculate);
btn_reset = findViewById(R.id.btn_reset);
txtBill = findViewById(R.id.txtBill);
txtTip = findViewById(R.id.txtTip);
txtSplit = findViewById(R.id.txtSplit);
txtRoundUp = findViewById(R.id.txtRoundUp);
txtNumOfSplits = findViewById(R.id.txtNumOfSplits);
txtTotalHeading = findViewById(R.id.txtTotalHeading);
txtTotalPerPerson = findViewById(R.id.txtTotalPerPerson);
txtTotalBill = findViewById(R.id.txtTotalBill);
txtTotalTip = findViewById(R.id.txtTotalTip);
txt_switch_error = findViewById(R.id.txt_switch_error);
switchRoundUp = (Switch) findViewById(R.id.switchRoundUp);
switchRoundDown = (Switch) findViewById(R.id.switchRoundDown);
edtBillAmount = findViewById(R.id.edtBillAmount);
horizontalLine = findViewById(R.id.horizontalLine);
这是你需要的吗?如果需要,您可以添加 double 值解析。
private static final String NUM_OF_SPLITS_KEY = "num_of_splits_key";
private static final String SELECTED_BUTTON_KEY = "selected_button_key";
private static final String TOTAL_PER_PERSON_KEY = "total_per_person_key";
private static final String TOTAL_BILL_KEY = "total_bill_key";
private static final String TOTAL_TIP_KEY = "total_tip_key";
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState: Started");
String textNumOfSplits = txtNumOfSplits.getText().toString();
int numOfSplits = Integer.parseInt(textNumOfSplits);
outState.putInt(NUM_OF_SPLITS_KEY, numOfSplits);
int selectedButtonId = 0;
if (btn_10.isSelected()) selectedButtonId = R.id.btn_10;
else if (btn_15.isSelected()) selectedButtonId = R.id.btn_15;
else if (btn_20.isSelected()) selectedButtonId = R.id.btn_20;
outState.putInt(SELECTED_BUTTON_KEY, selectedButtonId);
outState.putString(TOTAL_PER_PERSON_KEY, txtTotalPerPerson.getText().toString());
outState.putString(TOTAL_BILL_KEY, txtTotalBill.getText().toString());
outState.putString(TOTAL_TIP_KEY, txtTotalTip.getText().toString());
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "onRestoreInstanceState: Started");
int numOfSplits = savedInstanceState.getInt(NUM_OF_SPLITS_KEY);
txtNumOfSplits.setText(numOfSplits + "");
String selectedButtonId = savedInstanceState.getInt(SELECTED_BUTTON_KEY);
if (selectedButtonId == btn_10.getId()) selectButton(btn_10);
else if (selectedButtonId == btn_15.getId()) selectButton(btn_15);
else if (selectedButtonId == btn_20.getId()) selectButton(btn_20);
String totalPerPerson = savedInstanceState.getString(TOTAL_PER_PERSON_KEY);
txtTotalPerPerson.setText(totalPerPerson);
String totalBill = savedInstanceState.getString(TOTAL_BILL_KEY);
txtTotalBill.setText(totalBill);
String totalTip = savedInstanceState.getString(TOTAL_TIP_KEY);
txtTotalTip.setText(totalTip);
}
private void selectButton(@NonNull Button button) {
button.setSelected(true);
}
我试图保存屏幕旋转期间用户的所有值和点击,但我的按钮点击和文本视图不起作用。我花了很多时间试图弄清楚在这个方法中写什么,none 成功了。来自 Select 您的提示按钮和 textView 输出的输入目前未被保存。
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState: Started");
String text = txtNumOfSplits.getText().toString();
int no = Integer.parseInt(text);
outState.putInt("count", no);
String num_of_splits = txtNumOfSplits.getText().toString();
//Convert String to Integer
int no2 = Integer.parseInt(num_of_splits);
outState.putInt("count", no2);
money = Double.parseDouble(edtBillAmount.getText().toString());
outState.putString("bill", String.valueOf(money));
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "onRestoreInstanceState: Started");
String text = txtNumOfSplits.getText().toString();
int no = savedInstanceState.getInt("count");
txtNumOfSplits.setText(no + "");
money = Double.parseDouble(edtBillAmount.getText().toString());
String money = savedInstanceState.getString("bill");
txtTotalBill.setText("Total Bill\n$" + finalAmount.toString());
}
btn_add = findViewById(R.id.btn_add);
btn_subtract = findViewById(R.id.btn_subtract);
btn_20 = findViewById(R.id.btn_20);
btn_15 = findViewById(R.id.btn_15);
btn_10 =findViewById(R.id.btn_10);
btn_calculate = findViewById(R.id.btn_calculate);
btn_reset = findViewById(R.id.btn_reset);
txtBill = findViewById(R.id.txtBill);
txtTip = findViewById(R.id.txtTip);
txtSplit = findViewById(R.id.txtSplit);
txtRoundUp = findViewById(R.id.txtRoundUp);
txtNumOfSplits = findViewById(R.id.txtNumOfSplits);
txtTotalHeading = findViewById(R.id.txtTotalHeading);
txtTotalPerPerson = findViewById(R.id.txtTotalPerPerson);
txtTotalBill = findViewById(R.id.txtTotalBill);
txtTotalTip = findViewById(R.id.txtTotalTip);
txt_switch_error = findViewById(R.id.txt_switch_error);
switchRoundUp = (Switch) findViewById(R.id.switchRoundUp);
switchRoundDown = (Switch) findViewById(R.id.switchRoundDown);
edtBillAmount = findViewById(R.id.edtBillAmount);
horizontalLine = findViewById(R.id.horizontalLine);
这是你需要的吗?如果需要,您可以添加 double 值解析。
private static final String NUM_OF_SPLITS_KEY = "num_of_splits_key";
private static final String SELECTED_BUTTON_KEY = "selected_button_key";
private static final String TOTAL_PER_PERSON_KEY = "total_per_person_key";
private static final String TOTAL_BILL_KEY = "total_bill_key";
private static final String TOTAL_TIP_KEY = "total_tip_key";
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState: Started");
String textNumOfSplits = txtNumOfSplits.getText().toString();
int numOfSplits = Integer.parseInt(textNumOfSplits);
outState.putInt(NUM_OF_SPLITS_KEY, numOfSplits);
int selectedButtonId = 0;
if (btn_10.isSelected()) selectedButtonId = R.id.btn_10;
else if (btn_15.isSelected()) selectedButtonId = R.id.btn_15;
else if (btn_20.isSelected()) selectedButtonId = R.id.btn_20;
outState.putInt(SELECTED_BUTTON_KEY, selectedButtonId);
outState.putString(TOTAL_PER_PERSON_KEY, txtTotalPerPerson.getText().toString());
outState.putString(TOTAL_BILL_KEY, txtTotalBill.getText().toString());
outState.putString(TOTAL_TIP_KEY, txtTotalTip.getText().toString());
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "onRestoreInstanceState: Started");
int numOfSplits = savedInstanceState.getInt(NUM_OF_SPLITS_KEY);
txtNumOfSplits.setText(numOfSplits + "");
String selectedButtonId = savedInstanceState.getInt(SELECTED_BUTTON_KEY);
if (selectedButtonId == btn_10.getId()) selectButton(btn_10);
else if (selectedButtonId == btn_15.getId()) selectButton(btn_15);
else if (selectedButtonId == btn_20.getId()) selectButton(btn_20);
String totalPerPerson = savedInstanceState.getString(TOTAL_PER_PERSON_KEY);
txtTotalPerPerson.setText(totalPerPerson);
String totalBill = savedInstanceState.getString(TOTAL_BILL_KEY);
txtTotalBill.setText(totalBill);
String totalTip = savedInstanceState.getString(TOTAL_TIP_KEY);
txtTotalTip.setText(totalTip);
}
private void selectButton(@NonNull Button button) {
button.setSelected(true);
}