【问题标题】:All EditTexts are populated with data from one EditText after rotation and backpressed所有 EditText 都在旋转和后按后填充来自一个 EditText 的数据
【发布时间】:2014-10-03 03:53:40
【问题描述】:

我的应用程序正在使用风格化的 EditText 字段,这导致我在所需的每个布局中都使用自定义包含的 RelativeLayout 和 EditText。 EditTexts 都具有相同的 id,这似乎导致了这个问题: 我有一个包含多个 EditText 的表单,它们都具有相同的 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>

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

【问题讨论】:

    标签: java android fragment


    【解决方案1】:

    我通过将包含的 relativelayout 替换为允许我删除默认 EditText id 的 linearlayout 解决了这个问题。然后我以编程方式分配了 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> 
    

    【讨论】:

      猜你喜欢
      • 2015-01-07
      • 2011-03-11
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      相关资源
      最近更新 更多