目的:检查界面上所有的可输入控件都由用户输入了值。
控件较多,懒得一个一个地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;
}

解决方案 »

  1.   

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

  2.   


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

  3.   

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

  4.   

    测试OK。原因是不是因为inflater获取的那个对象和实际运行的那个Activity不是同一个啊??
      

  5.   

    但是我看文档中写的是:
    public abstract LayoutInflater getLayoutInflater ()Since: API Level 1
    Quick access to the LayoutInflater instance that this Window retrieved from its Context.照字面理解,应该获取的当前实际运行的那个context中的layout啊?
      

  6.   

    LayoutInflater是用来找layout下xml布局文件,并且实例化
    而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)参考这里
    http://weizhulin.blog.51cto.com/1556324/311450