做android开发的,想必痛恨findViewById很久了吧,为了解决这茬,无数框架涌现,有android-query-full、xUtils……无非利用反射、注解之类,通过ResourceId查找view,单从数据交互方面来说,这些不能满足我的需求,因为它们都需要手动为一个个view赋值/取值。
于是,我自己写了一个quick-form,可以给一个activity上所有的组件一次性赋值,不需要手动查找单个组件。我使用这个框架已有些日子,从中受益无数,最近很闲,决定分享给大家。
源码下载:https://github.com/Ice-Drinker/quick-form
使用方法:
首先,我们在activity里创建一组数据
ContentValues values = new ContentValues();
values.put("username", "tomcat");
values.put("sex", "man");
然后,在layout文件中
<com.ice.ui.MEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="username" />
<com.ice.ui.MRadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/no_radio_button"
android:orientation="horizontal"
android:tag="sex" >
<RadioButton
android:id="@id/no_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="women"
android:text="women" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:tag="man"
android:text="man" />
</com.ice.ui.MRadioGroup>
最后,在activity里赋值
IceTool.get().setMViewValues(getWindow().getDecorView(), values);
如图,大功告成。
自定义组件MEditText、MRadioGroup在源代码中有,源代码有完整例子。