zoukankan      html  css  js  c++  java
  • 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();
    }
    }



    }
    }

  • 相关阅读:
    js禁止鼠标右键功能
    js判断客户端是pc还是手机及获取浏览器版本
    js实现深拷贝的一些方法
    python使用requests请求的数据乱码
    PyCharm引入python需要使用的包
    几个常见用于解决nginx负载均衡的session共享问题的办法
    面试最让你手足无措的一个问题:你的系统如何支撑高并发?
    Linux/Windows 平台最容易安装 Composer教程
    Laravel一些常用命令整理
    nginx下重写隐藏index.php文件
  • 原文地址:https://www.cnblogs.com/lucoo/p/3732386.html
Copyright © 2011-2022 走看看