【Android开发】&利用Java反射获取、设置属性值来关联EditText数据

最近的这个项目app,是一个采集类的app。有很多张表,每一张表需要填写很多的字段值。那么填完之后,在交换界面可以为数据做修改,插入操作。创建数据库框架使用的是greendao。事先需要为每一张表编写一个bean。那么现在问题来了,这个bean每一个属性,需要和交互界面每一个EditText相匹配。试想这里有20多张表,看这工作量有多大。为此,在开发这个app之初,我特意要求我的小伙伴在编写每一个EditeText的ID必须为控件类型_表名_字段名来唯一区分每一个ID,同时每一张表格的bean自然就是每一属性对应表格的字段。

接下来看一下数据的插入操作。数据的插入得需要从交互界面获取值,那么一开始我是使用这样的方式来获取值得。如下面的代码。这里只是截取了一部分代码,是不是要崩溃,重复写类似的代码。因为这里只是一张表,一张表的插入操作,还没有涉及到更新。而更新的话,则需要从数据库里面取出值,然后填到EditeText上面、这样又是和一一的和每一个EditText编写代码。这工作量巨大。

entityPersonInfo.setPersoninfo_name(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_name));
                entityPersonInfo.setPersoninfo_sex(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_sex));
                entityPersonInfo.setPersoninfo_rename(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_rename));
                entityPersonInfo.setPersoninfo_picture(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_picture));
                entityPersonInfo.setPersoninfo_nationlity(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_nationlity));
                entityPersonInfo.setPersoninfo_catetype(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_catetype));
                entityPersonInfo.setPersoninfo_catecode(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_catecode));
                entityPersonInfo.setPersoninfo_catedate(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_catedate));
                entityPersonInfo.setPersoninfo_birthdate(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_birthdate));
                entityPersonInfo.setPersoninfo_heath(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_heath));
                entityPersonInfo.setPersoninfo_belief(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_belief));
                entityPersonInfo.setPersoninfo_marital(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_marital));
                entityPersonInfo.setPersoninfo_admincode(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_admincode));
                entityPersonInfo.setPersoninfo_birthadress(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_birthadress));
                entityPersonInfo.setPersoninfo_height(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_height));
                entityPersonInfo.setPersoninfo_nature(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_nature));
                entityPersonInfo.setPersoninfo_bloodtype(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_bloodtype));
                entityPersonInfo.setPersoninfo_age(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_age));

无奈,只要寻找其他方法。那么之前规定的每一个EditText以及bean字段是存在关联的,我们这里可以通过反射方式来获取bean类所有的属性,再将属性转为字符,再拼接其他字符,这个就可以组成EditText的ID,那么这样就可以来创建Bean(表)和交互界面的EditText关联。我们来看一下代码。那么这里只需要将上下文传进来,接着就是我们的表格,以及所有这些EditeText的父View。说白了,就是动态获取ID。那么现在就可以运用到其他的表格上面去了。下面这个函数是使用Java的反射,为属性设置值。

    /**
     *
     * @param context
     * @param bean
     * @param rootView
     */
    public static  void MatchSetEditTextToField(Context context, Object bean, View rootView){
        try {

            Class<?> cls=bean.getClass();
            Field[] fields=cls.getDeclaredFields();
            for (Field field:fields){
                String type = field.getType().getSimpleName();
                if (type.equals("String")){
                    String strBeanField=field.getName();
                    String edtID="edt_"+strBeanField;
                    int resId = context.getResources().getIdentifier(edtID, "id",context.getPackageName());
                    EditText edt=(EditText)rootView.findViewById(resId);
                    String value=edt.getText().toString();

                    field.set(bean,value);

                }
            }
        }catch (Exception e){
            Log.e("refect","反射的时候发生了错误");
        }
    }

我们刚才也说了,可以设置值的。这里使用的是读取EditText,然后将值填写到bean中,我们来看一下代码。

    public static void MatchSetFieldToEditText(Context context,  Object bean, View rootView){

        Class<?> cls=bean.getClass();
       try {
           Field[] fields=cls.getDeclaredFields();
           //personinfo_bloodtype
           String packageName="";
           for (Field field:fields ){

               String type = field.getType().getSimpleName();
               if (type.equals("String")){

                   String strBeanField=field.getName();
                   String edtID="edt_"+strBeanField;
                   int resId = context.getResources().getIdentifier(edtID, "id",context.getPackageName());
                   EditText edt=(EditText)rootView.findViewById(resId);
                   String value=String.valueOf(field.get(bean));
                   edt.setText(value);
               }
           }
       }catch (Exception e){
           e.fillInStackTrace();
       }

    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android中,可以通过设置EditText的Theme来设置其样式属性。Theme是一种视觉和样式的集合,可以通过在Theme中定义属性来改变控件的外观和行为。 要为EditText设置Theme,可以在布局文件的EditText标签中使用android:theme属性,指定一个样式资源,例如: ``` xml &lt;EditText android:id=&quot;@+id/edit_text&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;wrap_content&quot; android:theme=&quot;@style/MyEditTextTheme&quot; /&gt; ``` 在这里,我们指定了一个名为MyEditTextTheme的样式资源。 接下来,我们需要在样式资源文件中定义EditText的样式属性。例如,我们可以在styles.xml文件中定义MyEditTextTheme样式资源如下: ``` xml &lt;style name=&quot;MyEditTextTheme&quot; parent=&quot;@style/Widget.AppCompat.EditText&quot;&gt; &lt;item name=&quot;android:background&quot;&gt;@drawable/edit_text_bg&lt;/item&gt; &lt;item name=&quot;android:textColor&quot;&gt;@color/edit_text_text_color&lt;/item&gt; &lt;item name=&quot;android:textSize&quot;&gt;16sp&lt;/item&gt; &lt;/style&gt; ``` 在这里,我们使用parent属性指定了EditText的父样式,然后在样式中定义了EditText的背景、文本颜色和文本大小等属性。 需要注意的是,如果要为EditText设置Theme,需要使用支持Theme的EditText控件,例如使用AppCompat库的EditText控件。同时,为了确保代码的可读性和可维护性,最好将样式属性封装成一个单独的样式资源,方便复用和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yGIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值