java android布局里的控件值 反射绑定给实体类,实体类绑定给控件,表单提交绑定很有用
注意了:根据实际情况,添加实体里字段的类型,控件类型的判断才可使用。这里控件只有TextView EditText
实体类字段只有String int类型,带值的控件添加tag ,值和实体类的字段值一致
package ice.ui.service;
import java.lang.reflect.Field;
import
java.lang.reflect.InvocationTargetException;
import
java.lang.reflect.Method;
import java.lang.reflect.Type;
import
java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.view.ViewGroup;
import
android.widget.EditText;
import android.widget.TextView;
public class ViewModeTool {
/**
* get all views
*
* @param parentView
*
@return list
*/
public static synchronized List<View>
getAllViews(View parentView) {
List<View> ices = new
ArrayList<View>();
if (parentView instanceof ViewGroup) {
ViewGroup vp = (ViewGroup)
parentView;
for (int i = 0; i < vp.getChildCount(); i++) {
View viewchild =
vp.getChildAt(i);
if (viewchild instanceof ViewGroup)
{
ViewGroup vg = (ViewGroup)
viewchild;
ices.addAll(getAllViews(vg));
}else{
if(viewchild.getTag()!=null&&!viewchild.getTag().toString().equals(""))
ices.add(viewchild);
}
}
}
return ices;
}
public static void
GetVauleToMode(View parentView,Object moden
)
{
List<View> listv = getAllViews(parentView);
Class
classtype=moden.getClass();
for(int
i=0;i<listv.size();i++)
{
Object
Name=listv.get(i).getTag();
String Values="";
Field field =
null;
try {
if (listv.get(i) instanceof EditText)
{
EditText vg = (EditText)
listv.get(i);
Values=vg.getText().toString();
}else if
(listv.get(i)instanceof TextView) {
TextView vg = (TextView)
listv.get(i);
Values=(String) vg.getText();
}
field =
classtype.getDeclaredField(Name.toString());
field.setAccessible(true);
Type
type = field.getType();
if(type.equals(int.class))
{
int iv =
Integer.parseInt(Values);
field.setInt(moden, iv);
}else
if(type.equals(String.class))
{
field.set(moden,
Values);
}
} catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
} catch (NoSuchFieldException e)
{
// TODO Auto-generated catch
block
e.printStackTrace();
}
}
}
public static void GetVauleToMode2(View parentView,Object
moden )
{
List<View> listv =
getAllViews(parentView);
Class
classtype=moden.getClass();
for(int
i=0;i<listv.size();i++)
{
Object
Name=listv.get(i).getTag();
String Values="";
try
{
Method method=classtype.getMethod("set"+Name, new Class[]
{String.class });
if (listv.get(i) instanceof TextView)
{
TextView vg = (TextView) listv.get(i);
Values=(String)
vg.getText();
}else if (listv.get(i) instanceof EditText)
{
EditText vg = (EditText)
listv.get(i);
Values=vg.getText().toString();
}
method.invoke(moden,
new Object[]{Values});
} catch (NoSuchMethodException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
} catch
(IllegalArgumentException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
} catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InvocationTargetException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
}
public static void SetVauleToView(View
parentView,Object moden )
{
List<View> listv =
getAllViews(parentView);
Class
classtype=moden.getClass();
for(int
i=0;i<listv.size();i++)
{
Object
Name=listv.get(i).getTag();
Object Values="";
Field field =
null;
try {
field =
classtype.getDeclaredField(Name.toString());
field.setAccessible(true);
Values= field.get(moden);
if(Values!=null)
{
if (listv.get(i) instanceof EditText) {
EditText vg = (EditText)
listv.get(i);
vg.setText(Values.toString());
}else if (listv.get(i)
instanceof TextView) {
TextView vg = (TextView)
listv.get(i);
vg.setText(Values.toString());
}
}
}
catch (IllegalArgumentException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
} catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NoSuchFieldException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。