我试图在屏幕旋转期间保存用户的所有值和点击,但我的按钮点击和文本视图不起作用。我花了很多时间试图弄清楚在这个方法中写什么,但都没有奏效。来自 Select your Tip 按钮的输入和 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);