java android布局裡的控制項值 反射綁定給實體類,實體類綁定給控制項,表單提交綁定很有用

來源:互聯網
上載者:User

標籤:android   style   class   c   java   tar   

 

注意了:根據實際情況,添加實體裡欄位的類型,控制項類型的判斷才可使用。這裡控制項只有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());
}elseif (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();
}
}



}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.