我的片段1 XML佈局恢復有:查看知名度是沒有得到從堆棧中
- 兩個單選按鈕,默認情況下選中
- 兩個EditTexts(一用的inputType =「十進制」和其他與的inputType =「號「),具有可識別性=」 水漲船高」
- 按鈕,也能見度= 「水漲船高」
所以,起初他們似乎是這樣的:
然後在單擊任一個單選按鈕時,所需的EditText和按鈕將變爲可見,並且我可以輸入值。
在點擊按鈕,片段1被替換爲Fragment2。 然後在按下後退鍵時,後退堆棧不會恢復EditText和Button的可見性信息。它顯示所選的單選按鈕,並且只有在單擊它之後,我才能看到帶有先前輸入值的EditText和按鈕。
我希望它顯示與按鈕一起輸入的數值,而無需點擊單選選項。
我的片段的java:
public class Fragment1 extends Fragment {
// Member variables to store the visibility info of the two EditTexts and the button
int visibilityEditTextArea;
int visibilityEditTextNum;
int visibilityBtnEnterStateRec;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
// OnClickListener for the first radio button, "Area of Farm".
// Sets the visibility of the EditText with inputType="decimal" and the button to be VISIBLE,
// and intializes the member variables with the new visibility info.
RadioButton radioButtonArea = (RadioButton) view.findViewById(R.id.radio_area_of_farm_to_fertilize);
radioButtonArea.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = getActivity();
EditText editTextArea = (EditText) activity.findViewById(R.id.editText_area);
if (editTextArea.getVisibility() == View.GONE) {
editTextArea.setVisibility(View.VISIBLE);
visibilityEditTextArea = editTextArea.getVisibility();
}
EditText editTextNum = (EditText) activity.findViewById(R.id.editText_number);
if (editTextNum.getVisibility() == View.VISIBLE) {
editTextNum.setVisibility(View.GONE);
visibilityEditTextNum = editTextArea.getVisibility();
}
Button btnEnterStateRec = (Button) activity.findViewById(R.id.btn_enter_state_rec);
if (btnEnterStateRec.getVisibility() == View.GONE) {
btnEnterStateRec.setVisibility(View.VISIBLE);
visibilityBtnEnterStateRec = btnEnterStateRec.getVisibility();
}
}
});
// Similarly for the second radio button.
RadioButton radioButtonNum = (RadioButton) view.findViewById(R.id.radio_number_of_plants);
radioButtonNum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = getActivity();
EditText editTextArea = (EditText) activity.findViewById(R.id.editText_area);
if (editTextArea.getVisibility() == View.VISIBLE) {
editTextArea.setVisibility(View.GONE);
visibilityEditTextArea = editTextArea.getVisibility();
}
EditText editTextNum = (EditText) activity.findViewById(R.id.editText_number);
if (editTextNum.getVisibility() == View.GONE) {
editTextNum.setVisibility(View.VISIBLE);
visibilityEditTextNum = editTextArea.getVisibility();
}
Button btnEnterStateRec = (Button) activity.findViewById(R.id.btn_enter_state_rec);
if (btnEnterStateRec.getVisibility() == View.GONE) {
btnEnterStateRec.setVisibility(View.VISIBLE);
visibilityBtnEnterStateRec = btnEnterStateRec.getVisibility();
}
}
});
// OnClickListener for the Button, "ENTER STATE RECOMMENDATIONS".
// Puts the member variables in a bundle, and then replaces the container with Fragment2
Button buttonEnterStateRec = (Button) view.findViewById(R.id.btn_enter_state_rec);
buttonEnterStateRec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Puts the member variables in a bundle before making fragment transaction
Bundle outState = new Bundle();
outState.putInt("visibility_eT_area", visibilityEditTextArea);
outState.putInt("visibility_eT_num", visibilityEditTextNum);
outState.putInt("visibility_btn_enterStateRec", visibilityBtnEnterStateRec);
Fragment1 fragment1 = new Fragment1();
fragment1.setArguments(outState);
Fragment2 fragment2 = new Fragment2();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment2);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}
});
// Restores the member variables with the arguments from the bundle
Bundle args = this.getArguments();
if (args != null) {
visibilityEditTextArea = args.getInt("visibility_eT_area");
visibilityEditTextNum = args.getInt("visibility_eT_num");
visibilityBtnEnterStateRec = args.getInt("visibility_btn_enterStateRec");
// Checks if the int value of the member variable equals to that of View.VISIBLE.
// If it does then sets the respective View to be visible.
if (visibilityEditTextArea == View.VISIBLE) {
view.findViewById(R.id.editText_area).setVisibility(View.VISIBLE);
}
if (visibilityEditTextNum == View.VISIBLE) {
view.findViewById(R.id.editText_number).setVisibility(View.VISIBLE);
}
if (visibilityBtnEnterStateRec == View.VISIBLE) {
view.findViewById(R.id.btn_enter_state_rec).setVisibility(View.VISIBLE);
}
}
return view;
}
// Overides onPause() to save the visibility arguments in a bundle.
@Override
public void onPause() {
super.onPause();
Bundle outState = new Bundle();
outState.putInt("visibility_eT_area", visibilityEditTextArea);
outState.putInt("visibility_eT_num", visibilityEditTextNum);
outState.putInt("visibility_btn_enterStateRec", visibilityBtnEnterStateRec);
Fragment1 fragment1 = new Fragment1();
fragment1.setArguments(outState);
}
}
的XML是這樣的。我已經刪除的識別碼與佈局對齊的詳細資料。:
<RelativeLayout
android:focusable="true"
android:focusableInTouchMode="true">
...
<RadioGroup>
<RadioButton
android:text="Area of farm to fertilize" />
<RadioButton
android:text="Number of plants or trees" />
</RadioGroup>
<android.support.design.widget.TextInputLayout>
<EditText
android:hint="In hectare"
android:inputType="numberDecimal"
android:visibility="gone"
tools:visibility="visible" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout>
<EditText
android:inputType="number"
android:visibility="gone"
tools:visibility="visible" />
</android.support.design.widget.TextInputLayout>
<Button
android:text="Enter state recommendations"
android:visibility="gone"
tools:visibility="visible" />
</RelativeLayout>
感謝您提及標籤參數。它從我的add()中丟失。 (更多關於片段標籤在這裏的重要性的信息:http://stackoverflow.com/a/13510619/1944049)儘管如此,它仍然不能恢復可見性狀態。我想我必須改變我的設計。 –