嗨,伙计们,我用一个带有默认值的JSON动态创建了一些EditTexts。
在向EditTexts输入一些值之后,我使用根视图的getChildAt()方法访问它们。当使用getText() EditText方法时,它返回的是默认值,而不是更改后的值。我甚至无法通过代码设置EditText的文本。
这是我创建EditText的方法。
private static EditText createEditText(final Context context,
Element element) {
final EditText editText = new EditText(context);
editText.setId(Integer.parseInt(element.getFieldID()));
editText.setText(element.getCellValue());
editText.setTag(element);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (event != null
&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
editText.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
return editText;
}在更改文本之后,我就是这样访问EditTexts的。
public static boolean updateElements(ScrollView rootView) {
try {
for (int i = 0; i < rootView.getChildCount(); i++) {
View view = rootView.getChildAt(i);
if (view.getClass() == LinearLayout.class) {
LinearLayout layout = (LinearLayout) view;
for (int x = 0; x < layout.getChildCount(); x++) {
View linearView = layout.getChildAt(x);
if (linearView.getClass() == EditText.class) {
EditText txtBox = (EditText) linearView;
txtBox.setText("TextBox " + x);
}
}
}
}
}
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}请告诉我我做错了什么..。谢谢。
发布于 2012-06-27 06:30:01
终于找到问题了!
我一直在onCreate()和onResume()方法中设置活动的根视图。传递给updateElements(ScrollView rootView)的视图是在onCreate()方法中创建的视图;但实际显示的视图是在onResume()上创建的视图。
一个新手的错误!不管怎样,谢谢你们的帮助。
发布于 2012-06-26 12:17:08
试着改变
Object.getClass() == Class.class至
Object.getClass().equals(Class.class)https://stackoverflow.com/questions/11206996
复制相似问题