我遇到了我的Android應用程序的問題。我有兩個編輯文本和兩個由幾個單選按鈕組成的radiogroups。我有一個帶有碎片的導航抽屜。Android旋轉錯誤
當用戶鍵入數字並選擇單選按鈕時,它將該數字更改爲最終數字。但是,當Android設備旋轉到水平狀態時,單選按鈕將重置爲初始選中狀態,並重置edittext值。我希望它使edittext值和單選按鈕保持不變。
public class Fragment1 extends Fragment {
public static RadioButton button1a;
public static RadioButton button2a;
public static RadioButton button3a;
public static RadioButton button4a;
public static RadioButton button5a;
public static RadioButton button1b;
public static RadioButton button2b;
public static RadioButton button3b;
public static RadioButton button4b;
public static RadioButton button5b;
public static EditText valueinput;
public static EditText valueoutput;
public static double valueInputvar;
public static double valueOutputvar;
public static RadioGroup g1, g2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if (savedInstanceState != null) {
valueinput.setText(savedInstanceState.getString("input"));
valueinput.setText(savedInstanceState.getString("output"));
g1.check(savedInstanceState.getInt("n1"));
g2.check(savedInstanceState.getInt("n2"));
}
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("input", valueinput.getText().toString());
outState.putString("output", valueoutput.getText().toString());
outState.putInt("n1", g1.getCheckedRadioButtonId());
outState.putInt("n2", g2.getCheckedRadioButtonId());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View content = inflater.inflate(R.layout.fragment1_layout, container, false);
g1 = (RadioGroup) content.findViewById(R.id.radioGroup1);
g2 = (RadioGroup) content.findViewById(R.id.radioGroup2);
button1a = (RadioButton) content.findViewById(R.id.radio1a);
button2a = (RadioButton) content.findViewById(R.id.radio2a);
button3a = (RadioButton) content.findViewById(R.id.radio3a);
button4a = (RadioButton) content.findViewById(R.id.radio4a);
button5a = (RadioButton) content.findViewById(R.id.radio5a);
button1b = (RadioButton) content.findViewById(R.id.radio1b);
button2b = (RadioButton) content.findViewById(R.id.radio2b);
button3b = (RadioButton) content.findViewById(R.id.radio3b);
button4b = (RadioButton) content.findViewById(R.id.radio4b);
button5b = (RadioButton) content.findViewById(R.id.radio5b);
valueinput = (EditText) content.findViewById(R.id.value1);
valueoutput = (EditText) content.findViewById(R.id.value2);
valueinput.setHint("Enter a number");
valueinput.setText(df.format(1));
valueoutput.setText(df.format(1000));
valueInputvar = Double.parseDouble(valueinput.getText().toString());
valueOutputvar = Double.parseDouble(valueoutput.getText().toString());
valueInputvar = 0;
valueOutputvar = 0;
});
return content;
}
}
旋轉屏幕後,將調用onCreate方法,以便所有的視圖值都將重新設置。要保存視圖的最後值(例如EditText,RadioButton ...),您必須使用onSaveInstanceState。要獲取保存在onSaveInstanceState上的值,您需要使用onRestoreInstanceState。 – 2015-02-11 16:08:18