3

對於縱向和橫向模式,我有不同的佈局。 layout\activity_main.xml當針對不同的方向使用不同的佈局時,在方向更改時保存片段狀態

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout android:id="@+id/container" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"/> 

</RelativeLayout> 

而且layout-large\activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <fragment 
     android:id="@+id/fragment1" 
     android:name="com.example.fragmentorientationchangetest.Fragment1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

    <fragment 
     android:id="@+id/fragment2" 
     android:name="com.example.fragmentorientationchangetest.Fragment2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

</LinearLayout> 

我的片段是相同的,和fragment2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Fragment 2" /> 

    <EditText android:id="@+id/input" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="4dp" 
     android:layout_below="@+id/text"/> 
</RelativeLayout> 

我要的是,當我在編輯文本在我的片段輸入一些文字並且我旋轉屏幕,編輯文本不會丟失其輸入文本。

由於縱向和橫向模式下的片段不是一個實例,因此我不能使用setRetaintInstanceonSaveInstanceState。我怎樣才能做到這一點,而不需要定義兩個活動,並且不使用onConfigurationChanged

注意:不添加任何代碼,每個片段保存其數據。所以如果我在肖像模式下在fragment2中輸入一些文字,然後再旋轉到橫向並再次旋轉到肖像模式,我會看到輸入的文字。

+0

「我想要的是,當我在片段中的編輯文本中輸入一些文本時,旋轉屏幕,編輯文本不會丟失其輸入文本。」和「注意:不添加任何代碼,每個片段都會保存其數據,因此,如果我在肖像模式下輸入片段2中的某些文本,並再次旋轉到橫向並再次旋轉到肖像模式,則會看到輸入的文本。 **所以有什麼問題**和**方面注意:如果你要編輯你的問題添加編輯或更新後一些人回答** – mmlooloo 2014-10-03 06:49:50

+0

@ mmlooloo我在肖像模式。我在編輯文本中輸入一些文本,然後旋轉到橫向。我沒有看到輸入的文本,然後再次將屏幕旋轉到縱向模式,並看到輸入的文本。我應該怎麼做才能看到從陸地到港口和反過來的文字? – 2014-10-03 07:16:34

+0

因爲你的片段不是一個實例,你的問題實際上屬於片段通信類別,我認爲我的解決方案是最簡單的。你可以等待其他人,但我的解決方案就是我的答案。 – mmlooloo 2014-10-03 07:26:39

回答

0

正如我的代碼更我有時會忽略簡單的想法,如:

SharedPreferences myText = getSharedPreferences("txt", 0); 
SharedPreferences.Editor editor = settings.edit(); 
editor.putString("userText", "Your text that you want to save"); 
editor.commit(); 

和閱讀:

SharedPreferences myText = context.getSharedPreferences("txt", 0); 
String userInput = myText .getString("userText", ""); 

然後根據userInput設置你的EDITTEXT。

+0

我知道可以使用其他組件,例如'SharedPreferences'或'BroadCastReceiver'或者對片段實例有靜態引用。但我認爲他們不是好主意,因爲他們不是爲這種情況而製造的。 – 2014-10-03 06:03:47

+0

沒有'onSaveInstanceState'和'setRetaintInstance'你最後的選擇是'SharedPreferences'你提到的其他組件並不適合,也有危險就像「有靜態引用片段實例」 – mmlooloo 2014-10-03 07:06:52

+0

然而,我通過在主要使用'onSaveInstanceState'活動不是通過檢查哪個片段正在顯示片段,我認爲必須有另一個方便的方法來做到這一點。 – 2014-10-03 07:36:05

相關問題