2016-02-13 59 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的逻辑将为您恢复碎片,因此无所作为。