2016-02-13 37 views
1

我正在使用材質設計下的EditText,並且在屏幕方向有問題。我能夠保存onSavedInstanceState的內容,並將edittext上寫的內容重新恢復回來,但似乎有一個類似錯誤的行爲正在進行。EditText內容獲取jiberrish並重疊以前的內容的方向更改

看看以下這些圖片:

原稿方向時,活動開始: enter image description here

當我旋轉屏幕,我仍然得到內容的保持,但是當我放置CARRET上的現有文本跨度,它返回CARRET爲0(編輯文本的開始),然後當我輸入的東西,它看起來是這樣的:

enter image description here

我encountere這種情況曾經發生過很久以前。 IIRC我能夠通過完全替換內容onCreateView的savedInstanceState來解決它,它似乎不再工作。

任何想法?

[編輯] 很多人會期待回答「ONCONFIGURATIONCHANGED」。不,不,這不僅僅是解決潛在問題的方法。事實上,我嘗試過,並沒有工作,不想實施解決方案。我相信這不能保證樂隊援助解決方案,至少作爲最後的手段。

閱讀這個問題的答案:Why not use always android:configChanges=「keyboardHidden|orientation」?

下面是「碎片」包含編輯文本我的佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical" 
    tools:context=".HomeScreenActivity" 
    android:weightSum="1"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar_note" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     android:fitsSystemWindows="true"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsingtoolbarlayout_note" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed" 
      app:expandedTitleMarginStart="48dp" 
      app:expandedTitleMarginEnd="64dp"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar_note" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/colorPrimary" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
       app:contentInsetLeft="0dp" 
       app:contentInsetStart="0dp" 
       app:layout_collapseMode="pin"> 
       .... 
      </android.support.v7.widget.Toolbar> 

     </android.support.design.widget.CollapsingToolbarLayout> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <EditText 
       android:id="@+id/edittext_note" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="6" 
       android:gravity="top" 
       android:freezesText="true" 
       android:layout_marginLeft="16dp" 
       android:layout_marginStart="16dp" 
       android:layout_marginEnd="16dp" 
       android:layout_marginRight="16dp" 
       android:layout_marginTop="16dp" /> 

      <include 
       layout="@layout/layout_rte_toolbar_black_icon" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:gravity="bottom" 
       android:layout_weight="0.60"/> 

     </LinearLayout> 

    </android.support.v4.widget.NestedScrollView> 

</android.support.design.widget.CoordinatorLayout> 

下一頁這是我的活動佈局,一個非常簡單的佈局實際上是:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/framelayout_note" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

下面是創建方法回調的活動:

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    Log.i("NoteActivity.OnCreate" , TAG); 

    setContentView(R.layout.activity_note); 

    NoteFragment fragment = NoteFragment.createInstance(); 
    fragment.setOnToolbarButtonSelected(this); 

    getSupportFragmentManager() 
      .beginTransaction() 
      .add(R.id.framelayout_note , fragment) 
      .commit(); 
} 

下面是片段

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    Log.i("NoteFragment.onCreateView", TAG); 

    if(savedInstanceState != null) 
    { 
     mTitle = savedInstanceState.getString(KEY_EDITTEXT_TITLE, TAG); 
     mContent = Html.fromHtml(savedInstanceState.getString(KEY_EDITTEXT_CONTENT, TAG)); 
    } 

    mNoteDataController = ((NotifireApplication) getActivity().getApplication()).getNoteDataController(); 

    setHasOptionsMenu(true); 
    setRetainInstance(true); 
} 

@Override 
public View onCreateView(LayoutInflater inflater , ViewGroup parent, Bundle savedInstanceState) 
{ 
    Log.i("NoteFragment.onCreateView", TAG); 

    View view = inflater.inflate(R.layout.fragment_note, parent, false); 
    mNoteContent = (EditText) view.findViewById(R.id.edittext_note); 
    mNoteTitle = (EditText) view.findViewById(R.id.edittext_note_title); 

    if(mContent != null) mNoteContent.setText(mContent); 
    if(mTitle != null) mNoteTitle.setText(mTitle); 

    initToolbar(view); 

    return view; 
} 


@Override 
public void onSaveInstanceState(Bundle savedInstanceState) 
{ 
    Html.toHtml(mNoteContent.getText()); 

    savedInstanceState.putString(KEY_EDITTEXT_CONTENT, Html.toHtml(mNoteContent.getText())); 
    savedInstanceState.putString(KEY_EDITTEXT_TITLE, mNoteTitle.getText().toString()); 

    super.onSaveInstanceState(savedInstanceState); 
} 
+1

顯示您的佈局和活動? –

+0

@DougStevenson請參閱我的編輯 –

+0

是不是你的內容自動保存。我認爲你甚至不需要處理onSaveInstanceState。 –

回答

1

得到了問題。實際上,你所做的所有這些工作都應該在活動中完成。在這種情況下,因爲您使用的是框架佈局,我認爲發生的事情是您的舊片段就位,並且一旦您旋轉設備,就會創建一個新的片段實例並將其放置在其上方。

在我看來,這就是爲什麼你的舊文本在那裏,其實是在你以前的片段。我的意見是要麼處理配置更改,並且不要在方向更改上創建新的片段。

其次,如果你想測試我說的是你可以使用垂直方向的linearlayout,你會看到有兩個片段。

+0

感謝您的答案,但我主要關心的不是完全保留編輯文本或將頌歌放在需要的位置等。我正在解釋導致文本重疊其他文本的行爲。這是我的第二張照片的主要問題。 –

+0

檢查我的更新答案,這是模糊行爲的原因。 –

+0

你打敗了我!發現! –

-1

在首先創建/ onSavedInstanceState方法回調讓您編輯的文本數據轉換成一個全局變量,然後調用setContentView(),現在設定的數據到您的編輯文本。

這應該工作。

+0

感謝您的回答。但是,我不喜歡到ConfigurationChanged區域,這將是我的最後一招。它通常不用於解決這類問題。 –

+0

是的,我知道,但不知何故,這是我解決我的問題的方式。 – CandleCoder

1

當屏幕方向更改時,將創建兩個NoteFragment實例並將其添加到容器視圖中。這是你的根本問題。一個NoteFragment正在被FragmentActivity#onCreate創建和添加。另一個NoteFragment是在您的活動的onCreate回調中創建的,它的子類爲FragmentActivity。

您可以通過在onCreate中添加片段之前檢查savedInstanceState是否爲null來解決此問題。如果savedInstanceState爲空,則添加您的片段。如果沒有,那麼活動將在配置更改後重新創建,並且FragmentActivity的邏輯將爲您恢復碎片,因此無所作爲。