为什么遍历Activity layout中的控件时取不值

whatafuckinglongname 2011-10-30 05:44:07
目的:检查界面上所有的可输入控件都由用户输入了值。
控件较多,懒得一个一个地findViewById的方式来检查,想通过遍历界面上的控件的方式来实现。
但是,为啥可以获得id,但就是不能获取用户输入的值呢?
难道通过inflater获取到的只是同一个layout的新的实例???
求解啊?
暂时我只好把所有的R.id.xxx事先放到一个数组里,再一个一个地findViewById了。

private boolean checkForm(int layoutId) {
//RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(layoutId, null);
RelativeLayout layout = (RelativeLayout)LayoutInflater.from(this).inflate(layoutId, null);
layout = (RelativeLayout)findViewById(R.id.widget36);
boolean flag = true;

for (int i = 0; flag && i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
// Log.v("ID",v.getClass().getName().toString() + "," + v.getId());
flag = checkView(v);
}
return flag;
}

private boolean checkView(View v) {
boolean flag = true;
//! 为什么所有的控件都取不到值 !!!

Log.v("check", v.getClass().getName().toString() + "," + v.getId());

if ((v instanceof ImageView) || (v instanceof TextView)) {
return true;
} else if (v instanceof EditText) {
// Log.v("test", v.getId() + "EditText="
// +((EditText)v).getText().toString());
// if (((EditText) v).getText().toString().equals("")) {
// Log.v("return", v.getClass().toString());
// return false;
// }
EditText e = (EditText) findViewById(v.getId());
Log.v("test", "editText=" + e.getText().toString());
return true;
} else if (v instanceof Spinner) {
// Log.v("return",(((Spinner)v).getSelectedItemId() ==
// AdapterView.INVALID_ROW_ID) + "");
if (((Spinner) v).getSelectedItem() == null) {
Log.v("return", v.getClass().toString());
// ! SO DOES Spinner!!! CANNOT getValue!!
// return false;
return true;
}
} else if (v instanceof RadioGroup) {
if (((RadioGroup) v).getCheckedRadioButtonId() == -1) {
// Log.v("return", v.getClass().toString() + "," +
// ((RadioGroup)v).getCheckedRadioButtonId());
// ? return false;
return true;
}
} else if (v instanceof ViewGroup) {
Log.v("Recurve", v.getClass().toString());
for (int i = 0; flag && i < ((ViewGroup) v).getChildCount(); i++) {
View vx = ((ViewGroup) v).getChildAt(i);
flag = checkView(vx);
}
return flag;
}
Log.v("return2", v.getClass().toString());
return true;
}
...全文
323 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
周药师 2011-10-30
  • 打赏
  • 举报
回复
LayoutInflater是用来找layout下xml布局文件,并且实例化
而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)

参考这里
http://weizhulin.blog.51cto.com/1556324/311450
  • 打赏
  • 举报
回复
但是我看文档中写的是:
public abstract LayoutInflater getLayoutInflater ()

Since: API Level 1
Quick access to the LayoutInflater instance that this Window retrieved from its Context.

照字面理解,应该获取的当前实际运行的那个context中的layout啊?
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhouzhangkui 的回复:]

Java code

RelativeLayout loginLayout =(RelativeLayout)findViewById(R.id.relativeLayout1); //(RelativeLayout) getLayoutInflater().inflate(R.layout.oneactivity, null);
……
[/Quote]
测试OK。

原因是不是因为inflater获取的那个对象和实际运行的那个Activity不是同一个啊??
周药师 2011-10-30
  • 打赏
  • 举报
回复


private boolean checkForm(int layoutId) {
//你的代码里用这个就可以了
RelativeLayout layout = (RelativeLayout)findViewById(layoutId);

周药师 2011-10-30
  • 打赏
  • 举报
回复

RelativeLayout loginLayout =(RelativeLayout)findViewById(R.id.relativeLayout1); //(RelativeLayout) getLayoutInflater().inflate(R.layout.oneactivity, null);
for (int i = 0; i < loginLayout.getChildCount(); i++) {
View v1=loginLayout.getChildAt(i);
if(v1 instanceof EditText ){
Log.v("check", v1.getClass().getName().toString() + "," + v1.getId());
EditText e = (EditText)findViewById(v1.getId());
Log.v("test", "editText=" + e.getText().toString());
}
}

  • 打赏
  • 举报
回复
另外补充一下:界面是relativeLayout中嵌套了ScrollView,scrollView里又嵌套了TableLayout,所以在checkView的最后那个if-else中有递归判断。

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧