在方向改变(旋转屏幕)的EditText都显示相同的值,因为它们共享相同的ID [英] On orientation change (screen rotate) EditText all show the same value because they share the same id

查看:195
本文介绍了在方向改变(旋转屏幕)的EditText都显示相同的值,因为它们共享相同的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML定义,我编程实例多次,这取决于从数据库中的项目数。每个实例是一个的TableRow 这是后来加入 TableLayout 。在 TableLayout 在其他地方定义(在活动XML定义)。

I have the following XML definition that I programmatically instantiate many times, depending on the number of items from a database. Each instance is a TableRow that is later added to a TableLayout. The TableLayout is defined elsewhere (in the activity XML definition).

group_editblinds.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/group_editblindschedule"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="10dip" >

    <TextView
      android:id="@+id/textView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Round" />

    <EditText
      android:id="@+id/editText2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:ems="2"
      android:hint="Small"
      android:inputType="number"
      android:layout_weight="4"
      android:layout_marginRight="10dip"
      android:text="0" />

    <EditText
      android:id="@+id/name"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:ems="2"
      android:hint="Big"
      android:inputType="number"
      android:layout_weight="4"
      android:layout_marginRight="10dip"
      android:paddingRight="6dip"
      android:text="0" >
    </EditText>

    <CheckBox
      android:id="@+id/checkBox1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" />

</TableRow>

我从其他的答案读了,因为我已经实例化了同样的观点多次,每个视图包含相同的ID。

I have read from other answers that because I have instantiated the same view many times, each view contains the same Id.

我的活动为pretty简单。唯一的额外部分是,它有一个用户定义的对象, BlindSchedule ,用于填充 EditView中秒。这必须在方向改变被保留。你可以把这个活动作为一个数据库,这取决于我们如何到了活动创建/更新。

My activity is pretty simple. The only extra-piece is that it has a user-defined object, BlindSchedule, that is used to populate the EditViews. This must be retained on orientation change. You can think of this activity as a database create/update depending on how we got to the activity.

public class EditBlindScheduleActivity extends Activity {

  private TableLayout table;
  private BlindSchedule bs;
  private EditText name;

  boolean creating;

  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_blind_schedule);

    if (savedInstanceState == null)
    {
      bs = (BlindSchedule) getIntent().getSerializableExtra("blindSchedule");
    }

    table = (TableLayout) findViewById(R.id.editblindtable);
    name = (EditText) findViewById(R.id.nameEdit);

    if (bs == null) {
      creating = true;
    } else {
      creating = false;
    }

    if (creating) 
    {
      setTitle("Create Blind Schedule");

      for(int i = 1; i < 26; ++i)
      {
        TableRow row = (TableRow) View.inflate(this, R.layout.group_editblindschedule, null);
        TextView tv = (TextView) row.getChildAt(0);
        tv.setText("" + i);
        table.addView(row);
      }
    } 
    else 
    {
      setTitle("Edit Blind Schedule");

      name.setText(bs.getName());

      int i = 1;
      for (BlindLevel level : bs.getBlindLevels())
      {
        TableRow row = (TableRow) View.inflate(this, R.layout.group_editblindschedule, null);
        TextView tv = (TextView) row.getChildAt(0);
        tv.setText("" + i);
        EditText et = (EditText) row.getChildAt(1);
        et.setText("" + level.getSmallBlind());
        et = (EditText) row.getChildAt(2);
        et.setText("" + level.getBigBlind());
        CheckBox cb = (CheckBox) row.getChildAt(3);
        cb.setChecked(true);
        table.addView(row);
        ++i;
      }
    }
  }

  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      bs = (BlindSchedule) savedInstanceState.getSerializable("blindSchedule");
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    if (bs != null) {
      outState.putSerializable("blindSchedule", bs);
    }
    super.onSaveInstanceState(outState);
  }
}

当我到达活动编辑BlindSchedule,值看起来是这样的:

When I get to the activity to edit a BlindSchedule, the values look something like:

1 2
2 4 
4 8
16 32
100 200

当我旋转屏幕,我得到:

When I rotate the screen, I get:

100 200
100 200
100 200
100 200
100 200

同样,我相信这是因为我的观点有相同的ID,由于这一事实,即相同的XML资源被多次实例化。什么是解决这个烦恼的正确方法?

Again, I believe this is because all my views have the same Id due to the fact that the same XML resource was instantiated multiple times. What is the correct way to fix this annoyance?

推荐答案

我维伦Melkumyan同意,你应该使用的ListView 的ListView 适配器可以先恐吓,但他们将被证明是强大的工具,当你学会使用它们。

I agree with Vilen Melkumyan that you should use a ListView. ListView and Adapter can be intimidating at first, but they will prove to be powerful tools when you learn to use them.

要回答你的问题,如果你想坚持使用 TableLayout ,您可以添加以下到的Andr​​oidManifest.xml <你的活动/ code>文件:

To answer your question, if you want to stick with the TableLayout, you can add the following to your activity in the AndroidManifest.xml file:

            android:configChanges="keyboardHidden|orientation|screenSize" >

这将prevent的活动来进行定向状态改变后重新载入。

This will prevent the Activityto be reloaded after the orientation state changes.

这篇关于在方向改变(旋转屏幕)的EditText都显示相同的值,因为它们共享相同的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆