应无所住,而生其心
排名
1
文章
860
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

android 布局文件里输入框的值自动转换到类里边,可以增加为空验证

4572人阅读 2016/3/11 17:35 总访问:5199724 评论:0 收藏:0 手机
分类: 移动开发

android 布局文件里输入框的值自动转换到类里边,不用一个一个去读取在赋值
先看看android遍历view子控件,用回调函数

  1. /**
  2. * 遍历所有view
  3. *
  4. * @param viewGroup
  5. */
  6. public void traversalView(ViewGroup viewGroup) {
  7. int count = viewGroup.getChildCount();
  8. for (int i = 0; i < count; i++) {
  9. View view = viewGroup.getChildAt(i);
  10. if (view instanceof ViewGroup) {
  11. traversalView((ViewGroup) view);
  12. } else {
  13. doView(view);
  14. }
  15. }
  16. }
  17. /**
  18. * 处理view
  19. *
  20. * @param view
  21. */
  22. private void doView(View view) {
  23. }

一:方法1 遍历所有子view,得到tag反射+泛型去赋值

  1. private <T> T traversalView(ViewGroup viewGroup,Class<T> _t)
  2. {
  3. try {
  4. T t = _t.newInstance();
  5. traversalViewTemp(viewGroup,t);
  6. return t;
  7. }
  8. catch (Exception e) {
  9. System.out.println("自动为类添加值报错:"+e.getMessage());
  10. return null;
  11. }
  12. }
  13. private <T> T traversalViewTemp(ViewGroup viewGroup,T t)
  14. {
  15. try {
  16. int count = viewGroup.getChildCount();
  17. for (int i = 0; i < count; i++) {
  18. View view = viewGroup.getChildAt(i);
  19. if (view instanceof ViewGroup) {
  20. traversalViewTemp((ViewGroup) view,t);
  21. } else {
  22. doView(view,t);
  23. }
  24. }
  25. return t;
  26. } catch (Exception e) {
  27. System.out.println("自动为类添加值报错:"+e.getMessage());
  28. return null;
  29. }
  30. }
  31. private <T> void doView(View view,T t) {
  32. try
  33. {
  34. if(view.getTag()!=null &&!view.getTag().equals(""))
  35. {
  36. String pname = view.getTag().toString();
  37. System.out.println("得到的属性:"+pname);
  38. if(view instanceof EditText) //这里只读EditText,可以根据需求变化
  39. {
  40. EditText ex = (EditText)view;
  41. if(ex.getText()!=null){
  42. Field f = t.getClass().getField(pname);
  43. f.setAccessible(true);//忽略访问符private也可以访问
  44. f.set(t,ex.getText().toString());
  45. }
  46. }
  47. }
  48. } catch (Exception e) {
  49. System.out.println("自动为类添加值报错:"+e);
  50. }
  51. }

ManualAddOil类:

  1. public class ManualAddOil {
  2. private String oil_type;
  3. private String shipname;
  4. public String getOil_type() {
  5. return oil_type;
  6. }
  7. public void setOil_type(String oil_type) {
  8. this.oil_type = oil_type;
  9. }
  10. public String getShipname() {
  11. return shipname;
  12. }
  13. public void setShipname(String shipname) {
  14. this.shipname = shipname;
  15. }
  16. }

使用:

  1. ManualAddOil mao = traversalView(manualdatetime,ManualAddOil.class);
  2. System.out.println("自动转化完后值Shipname:"+mao.getShipname()+",Oil_type:"+mao.getOil_type());

二:方法2 使用类的属性去匹配

遍历所有view可能会比较多,直接用类的属性去找view然后在赋值

  1. private <T> T traversalView(ViewGroup viewGroup,Class<T> _t)
  2. {
  3. try {
  4. T t = _t.newInstance();
  5. Field[] fids = t.getClass().getDeclaredFields();//所有属性,与访问修饰符无关
  6. for(int i=0;i<fids.length;i++)
  7. {
  8. Field f = fids[i];
  9. f.setAccessible(true);//忽略访问符private也可以访问
  10. String pname = f.getName();//得到属性名
  11. View view = viewGroup.findViewWithTag(pname);
  12. if(view!=null)
  13. {
  14. if(view instanceof EditText) //这里只读EditText,可以根据需求变化
  15. {
  16. EditText ex = (EditText)view;
  17. if(ex.getText()!=null){
  18. f.set(t,ex.getText().toString());
  19. }
  20. }
  21. }
  22. }
  23. return t;
  24. }
  25. catch (Exception e) {
  26. System.out.println("自动为类添加值报错:"+e.getMessage());
  27. return null;
  28. }
  29. }

使用方法完全一样

三:方法3 使用注解来配置需要转换的字段

1:定义一个注解

  1. import java.lang.annotation.Documented;
  2. import java.lang.annotation.Inherited;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.ElementType;
  5. import java.lang.annotation.Target;
  6. import java.lang.annotation.RetentionPolicy;
  7. @Target(ElementType.FIELD)/*目标:用于字段的注解*/
  8. @Retention(RetentionPolicy.RUNTIME)
  9. @Documented
  10. @Inherited
  11. public @interface AutoCAttribute {
  12. boolean isAutoConvert() default false;//是否需要自动转化
  13. }

2:在model上配置使用注解

  1. public class ManualAddOil {
  2. @AutoCAttribute(isAutoConvert=true)
  3. private String oil_type;
  4. //@AutoCAttribute(isAutoConvert=true)
  5. private String shipname;
  6. public String getOil_type() {
  7. return oil_type;
  8. }
  9. public void setOil_type(String oil_type) {
  10. this.oil_type = oil_type;
  11. }
  12. public String getShipname() {
  13. return shipname;
  14. }
  15. public void setShipname(String shipname) {
  16. this.shipname = shipname;
  17. }
  18. }

3:反射转换的时候运用注解

  1. public <T> T traversalView(ViewGroup viewGroup,Class<T> _t)
  2. {
  3. try {
  4. T t = _t.newInstance();
  5. Field[] fids = t.getClass().getDeclaredFields();//所有属性,与访问修饰符无关
  6. for(int i=0;i<fids.length;i++)
  7. {
  8. Field f = fids[i];
  9. f.setAccessible(true);//忽略访问符private也可以访问
  10. AutoCAttribute an = f.getAnnotation(AutoCAttribute.class);//得到特性
  11. if(an!=null && an.isAutoConvert())
  12. {
  13. BingValue(t,f,viewGroup);
  14. }
  15. }
  16. return t;
  17. }
  18. catch (Exception e) {
  19. System.out.println("自动为类添加值报错:"+e.getMessage());
  20. return null;
  21. }
  22. }
  23. private <T> void BingValue(T t,Field f,ViewGroup viewGroup) throws IllegalArgumentException, IllegalAccessException
  24. {
  25. String pname = f.getName();//得到属性名
  26. View view = viewGroup.findViewWithTag(pname);
  27. if(view!=null)
  28. {
  29. if(view instanceof EditText) //这里只读EditText,可以根据需求变化
  30. {
  31. EditText ex = (EditText)view;
  32. if(ex.getText()!=null){
  33. f.set(t,ex.getText().toString());
  34. }
  35. }
  36. }
  37. }

如果有些少数需要绑定隐藏的值的就直接得到,不用自动转换了

四:使用注解来配置需要取的是控件的text还是tag

因为有些是使用绑定的值
1:定义注解

  1. public @interface AutoCAttribute {
  2. boolean isAutoConvert() default true;//是否需要自动转化
  3. boolean isCanNull() default false;//是否能为空
  4. boolean isTagsecond() default false;//是否通过键值对形势的tag取值(原本的tag用来处理映射了)
  5. }

2:处理特性

  1. if(an.isTagsecond()==true)
  2. {
  3. f.set(t,ex.getTag(R.id.tag_second));
  4. }
  5. else
  6. {
  7. f.set(t,ex.getText().toString());
  8. }

这里是用得键值对的方式设置tag的

五:为空认证

  1. 有些简单的为空认证我们可以用特性来配置一下那些不能为空,为空了就抛出个异常给个提示不能为空,就不用一个一个去判断了

1:注解增加一个是否进行为空判断字段

  1. @Target(ElementType.FIELD)/*目标:用于字段的注解*/
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. public @interface AutoCAttribute {
  6. boolean isAutoConvert() default false;//是否需要自动转化
  7. boolean isCanNull() default false;//是否能为空
  8. }

2:自定义一个为空异常

  1. public class NotNullException extends Exception{
  2. public NotNullException(String msg)
  3. {
  4. super(msg);
  5. }
  6. }

3:增加一个抛出为空异常,使用的时候捕获相应的异常就行了

  1. private <T> void BingValue(T t,Field f,ViewGroup viewGroup,AutoCAttribute an) throws IllegalArgumentException, IllegalAccessException, NotNullException
  2. {
  3. String pname = f.getName();//得到属性名
  4. View view = viewGroup.findViewWithTag(pname);
  5. if(view!=null)
  6. {
  7. if((view instanceof EditText)) //这里只读EditText,TextView可以根据需求变化
  8. {
  9. EditText ex = (EditText)view;
  10. if(ex.getText()!=null){
  11. f.set(t,ex.getText().toString());
  12. }
  13. else
  14. {
  15. if(an.isCanNull()==false)
  16. {
  17. throw new NotNullException("你的信息没有填写完整!");
  18. }
  19. }
  20. }
  21. if((view instanceof TextView))
  22. {
  23. TextView ex = (TextView)view;
  24. if(ex.getText()!=null){
  25. f.set(t,ex.getText().toString());
  26. }
  27. else
  28. {
  29. if(an.isCanNull()==false)
  30. {
  31. throw new NotNullException("你的信息没有填写完整!");
  32. }
  33. }
  34. }
  35. }
  36. }

欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

解决android studio运行出现Session 'app': Error Installing APKs错误

之前项目好好的,早上打开突然报错Session &#39;app&#39;: Error Installing APKs解决方法:选择Build———— clean proje...

android使用MPandroidChart开源图表折线图

1. 将mpandroidchartlibrary-2-1-6.ja包copy到项目的libs中在引用2:布局文件&lt;com.github.mikephil.charting.charts.Line...

android 弹出选择框简单通用弹出选择框

制作一个简单通用的弹出选择框LinearLayoutll=(LinearLayout)getActivity().findViewById(R.id.log_sel_qyport); ll.set...

android studio打包脱坑1

打包时出现的&#39;:app:validateExternalOverrideSigning&#39;问题报错如下解决方法错误的原因是找不到这个签名文件。由于K...

javaandroid 使用socket.io-client连接nodejs websocket

socket.io-client相比SocketIO.jar使用起来更方便一点publicvoidconnection(finalMapAction_action){ try{ IO.Optionsopt...

javaandroid 使用SocketIO.jar连接nodejs websocket

socket.io-client版连接nodejs websockethttp://www.tnblog.net/aojiancc2/article/details/2562一:更具url建立连接,调用...

Xamarin: android.permission.CALL_PHONE 的权限问题

写个电话拨号器,很简单就一个Edittext和一个button,用来输入号码并且点击按钮拨打电话,但是写好以后报的是安全错误,我上...

android 漂亮的listview

效果如下:首先在drawable下定义选择器shape_bg_listview.xml 实现圆角:&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;...

android 获取导航栏的高度

获取Android手机屏幕的高度/** *获取状态栏高度 * *@paramcontext上下文 *@return高度 */publicstaticintgetStatusBar...

android 代码

一、判断WiFi是否打开1、注册权限//需注册权限android.permission.ACCESS_WIFI_STATE WifiManagerwifiManager=(WifiManage...

ionic cordova platform add android报错

ionic各种环境以及配置完但是添加平台报错:Using cordova-fetch for anroidFailed to fetch platform anroidProbably this...

android 布局实例解析 格子菜单效果

使用android权重布局,使每个格子和间隔在不同手机中自动适配: &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8...

android后台动态添加布局文件、控件与动态设置属性

翻译布局文件布局文件 &lt;LinearLayout android:layout_width=&quot;fill_parent&quot; andro...

android后台动态添加布局文件、控件与动态设置属性2

原布局文件 &lt;ScrollView android:layout_width=&quot;wrap_content&quot; android:layout_heig...

android 绘制自定义控件,android绘制同心圆,android绘制小三角,android画虚线

一:实现同心圆加小三角指向效果 同心圆public class RingView extends View{ private Paint paint; privat...