gpt4 book ai didi

android - 更改屏幕方向时 EditText 中的值错误

转载 作者:太空狗 更新时间:2023-10-29 15:35:24 26 4
gpt4 key购买 nike

我很困惑。

我有一个包含 TextView 和 EditText 的布局 (LinearLayout)。在我的代码中的某个地方,我(以编程方式)创建了一个 RelativeLayout 并将其中的几个 LinearLayouts 插入其中。对于它们中的每一个,我都为 TextView 和 EditText 设置了一个 ID 和值。

问题是当我改变屏幕方向时。所有 EditText 控件都获得相同的值(来自最后一个 EditText)。如果我设置断点,一切似乎都很好。我什至在调用 setContentView 之前检查了所有元素,所有值看起来都很好。

有什么想法吗?

这里是代码的相关部分:

@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);
}

最佳答案

我遇到了同样的问题。我不知道为什么会这样;它发生在我的一项 Activity 中,但没有发生在我认为几乎相同的另一项 Activity 中。不过,我还没有对其进行广泛调查。

但是,我想出了一个解决方法:onResume() 而不是 onCreate() 中填充 EditText。(我也试图在 onStart 中填充它们,但后来我遇到了同样的错误。)我希望这可能对您或阅读此线程的任何其他人有用。

关于android - 更改屏幕方向时 EditText 中的值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6745810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com