改变屏幕方向时的EditText错误值 [英] Wrong value in EditText when changing screen orientation

查看:104
本文介绍了改变屏幕方向时的EditText错误值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林困惑。

我有一个布局(LinearLayout中)拿着一个TextView和EditText上。
某处在我的$ C C I创建(编程)一个RelativeLayout的,并推了好这个LinearLayouts的。要他们每个人我设置ID和值的TextView和EditText上。

问题是,当我改变屏幕的方向。所有的EditText控制得到相同的值(从最后的EditText)。如果我设置一个断点一切似乎罚款。我甚至检查前的setContentView所有elemets被调用,所有的价值观似乎罚款。

任何想法?

下面是code的相关部分:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
  super.onCreate(savedInstanceState);
  csWgs84 = getCoordinateSystem();
  wgs​​84C = getCoordinates();
  sView =新的滚动型(本);
  布局= getLayoutInflater()膨胀(R.layout.location_calculator,NULL);  查看控制1 = getLayoutInflater()膨胀(R.layout.text_edit,NULL);
  control1.setId(1);
  TextView的TITLE1 =(TextView中)control1.findViewById(R.id.title);
  title1.setText(csWgs84.axisInfo.yAxisAbbreaviation +[°]:);
  的EditText值1 =(EditText上)control1.findViewById(R.id.input);
  value1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
  value1.setText(cutValue(RSimpleFormatter.formatNumber(wgs84C.y,1,7),12)'。');
  RelativeLayout.LayoutParams viewLayoutParams1 =新RelativeLayout.LayoutParams
    (RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
      viewLayoutParams1.addRule(RelativeLayout.BELOW,group.getId());
  ((RelativeLayout的)布局).addView(控制1,viewLayoutParams1);  查看控制2 = getLayoutInflater()膨胀(R.layout.text_edit,NULL);
  control2.setId(2);
  TextView的标题2 =(TextView中)control2.findViewById(R.id.title);
  title2.setText(csWgs84.axisInfo.xAxisAbbreaviation +[°]:);
  的EditText VALUE2 =(EditText上)control2.findViewById(R.id.input);
  value2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
  value2.setText(cutValue(RSimpleFormatter.formatNumber(wgs84C.x,1,7),12)'。');
  RelativeLayout.LayoutParams viewLayoutParams2 =新RelativeLayout.LayoutParams
  (RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
     viewLayoutParams2.addRule(RelativeLayout.BELOW,control1.getId());
    ((RelativeLayout的)布局).addView(控制2,viewLayoutParams2);  sView.addView(布局,scrollViewLayoutParams);
  的setContentView(sView);
}


解决方案

如果(公认通常是很聪明的)默认的onPause()方法是不是很好打交道的活动被推到内存和重新装载从中那么你需要覆盖它。

使用的onPause保存编程创建字段的值在一个包。在一个捆绑的presence您onCreate方法检查,并创造你认为的来源于此。

活动生命周期

Im confused.

I have a layout (LinearLayout) holding a TextView and EditText. Somewhere in my code i create (programatically) a RelativeLayout and push in it several of this LinearLayouts. To each one of them i set an ID and values for TextView and EditText.

The problem is when i change screen orientation. All EditText controls gets the same value (from the last EditText). If i set a breakpoint all seems fine. I even inspected all elemets before setContentView is called and all values seem fine.

Any idea?

Here is the relevant part of code:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  csWgs84 = getCoordinateSystem();
  wgs84C = getCoordinates();
  sView = new ScrollView(this);
  layout = getLayoutInflater().inflate(R.layout.location_calculator, null);

  View control1 = getLayoutInflater().inflate(R.layout.text_edit, null);
  control1.setId(1);
  TextView title1 = (TextView) control1.findViewById(R.id.title);
  title1.setText(csWgs84.axisInfo.yAxisAbbreaviation + " [°] :");
  EditText value1 = (EditText) control1.findViewById(R.id.input);
  value1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
  value1.setText(cutValue(RSimpleFormatter.formatNumber(wgs84C.y, 1, 7, '.'), 12));
  RelativeLayout.LayoutParams viewLayoutParams1 = new RelativeLayout.LayoutParams
    (RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT );
      viewLayoutParams1.addRule(RelativeLayout.BELOW, group.getId());
  ((RelativeLayout) layout).addView(control1, viewLayoutParams1);

  View control2 = getLayoutInflater().inflate(R.layout.text_edit, null);
  control2.setId(2);
  TextView title2 = (TextView) control2.findViewById(R.id.title);
  title2.setText(csWgs84.axisInfo.xAxisAbbreaviation + " [°] :");
  EditText value2 = (EditText) control2.findViewById(R.id.input);
  value2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
  value2.setText(cutValue(RSimpleFormatter.formatNumber(wgs84C.x, 1, 7, '.'), 12));
  RelativeLayout.LayoutParams viewLayoutParams2 = new RelativeLayout.LayoutParams
  (RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT );
     viewLayoutParams2.addRule(RelativeLayout.BELOW, control1.getId());
    ((RelativeLayout) layout).addView(control2, viewLayoutParams2);

  sView.addView(layout, scrollViewLayoutParams);
  setContentView(sView);
}

解决方案

If the (admittedly usually very clever) default onPause() method isn't dealing well your activity being pushed to memory and being reloaded from it then you need to override it.

Use onPause to save the value's of your programatically created fields in a bundle. In your onCreate method check for the presence of a Bundle and create you're view's from this.

Activity Lifecycle

这篇关于改变屏幕方向时的EditText错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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