旋转和反向后,所有EditTexts都使用来自一个EditText的数据填充

时间:2014-10-01 21:20:58

标签: java android fragment

我的应用程序正在使用程式化的EditText字段,这使我在所需的每个布局中使用自定义包含RelativeLayout和EditText。 EditTexts都具有相同的ID,似乎导致此问题: 我有一个包含多个EditTexts的表单都具有相同的ID,并且包含如下:

<include
    android:id="@+id/person_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/custom_edit_text" />

我覆盖了包含RelativeLayout的id,因此我可以稍后通过调用findViewById(R.id.person_name).findViewById(R.id.my_edit_text)来获取此editText。 下面是EditText布局的样式:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

<EditText
    android:id="@+id/my_edit_text"
    android:layout_width="match_parent"
    android:background="@color/White"
    android:inputType="textPersonName" >
    <requestFocus />
</EditText>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/my_edit_text"
    android:layout_below="@id/my_edit_text"
    android:src="@drawable/shadow_left" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@id/my_edit_text"
    android:layout_below="@id/my_edit_text"
    android:src="@drawable/shadow_right" />

</RelativeLayout>

如果用户输入了&#39;在其中一个EditTexts中,然后旋转此片段或从另一个屏幕导航回到它,所有包含的EditTexts显示相同的&#39; a&#39;文本。我已经尝试将所有字段数据保存在一个包中,并在onCreateView中加载savedInstanceState,但这只能解决旋转问题而不能导航回此片段。我可以手动在每个布局中使用编辑文本布局而不是使用包,但这将是最后的手段,因为它会使xml非常混乱。感谢。

1 个答案:

答案 0 :(得分:0)

我通过使用linearlayout替换包含的relativelayout解决了这个问题,允许我删除默认的EditText id。然后,我以编程方式调用

指定了EditTexts id
my_edit_text.setId(R.id.person_name);

并将ids.xml添加到我的res / values中,每个编辑文本都有唯一的ID。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="person_name" type="id"/>
</resources>