我在Material Design下使用EditText,我在屏幕方向上遇到问题。我能够保存onSavedInstanceState中的内容并重新恢复在edittext上写回的内容,但似乎存在类似错误的行为。
请看以下照片:
当我旋转屏幕时,我仍然可以抓住内容但是当我将carret放在现有的文本范围上时,它会将carret返回到0(编辑文本的开头),然后当我输入的内容看起来像这样:
我曾经遇到过这种情况很久很久以前。 IIRC我能够通过完全替换创建ViewInstanceState上的内容来解决它,它似乎不再起作用了。
有什么想法吗?
[编辑] 很多人都期待回答" 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>
接下来是我的Activity布局,实际上是一个非常简单的布局:
<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"/>
这是Activity的创建方法回调:
@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();
}
这是片段
的create / onSavedInstanceState方法回调@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);
}
答案 0 :(得分:1)
遇到了问题。实际上你在片段中所做的所有这些工作都应该在活动中完成。在这种情况下,因为您正在使用框架布局,我认为发生的事情是您的旧片段已就位,一旦您旋转设备,就会创建一个新的片段实例并将其放置在它上面。
在我看来,这就是为什么你的旧文本存在于你之前的片段中的原因。我的意见是处理配置更改并且不要在方向更改上创建新片段。
其次,如果你想测试我所说的是你可以使用垂直方向的linearlayout,你会发现有两个片段。
答案 1 :(得分:1)
当屏幕方向改变时,正在创建两个NoteFragment实例并将其添加到容器视图中。这是你的根本问题。 FragmentActivity#onCreate正在创建和添加一个NoteFragment。另一个NoteFragment正在您的活动的onCreate回调中创建,它是FragmentActivity的子类。
您可以通过在onCreate中添加片段之前检查savedInstanceState是否为null来解决此问题。如果savedInstanceState为null,则添加您的片段。如果没有,那么在配置更改后重新创建活动,FragmentActivity的逻辑将为您恢复碎片,所以什么都不做。
答案 2 :(得分:-1)
在onConfigurationChanged()
首先将编辑文本的数据转换为全局变量,然后调用setContentView()
,现在将数据设置为编辑文本。
这应该有用。