├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
└── runConfigurations.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── xyz
│ │ └── codedream
│ │ └── lib
│ │ └── form
│ │ └── sample
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── xyz
│ │ │ └── codedream
│ │ │ └── lib
│ │ │ └── form
│ │ │ └── sample
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── xyz
│ └── codedream
│ └── lib
│ └── form
│ └── sample
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── xyz
│ │ └── codedream
│ │ └── lib
│ │ └── form
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── xyz
│ │ │ └── codedream
│ │ │ └── lib
│ │ │ └── form
│ │ │ ├── AbstractFormField.java
│ │ │ ├── BaseViewFormField.java
│ │ │ ├── FieldOptions.java
│ │ │ ├── FieldResultWriter.java
│ │ │ ├── FormField.java
│ │ │ ├── FormLayout.java
│ │ │ ├── Validator.java
│ │ │ ├── ValueConvertor.java
│ │ │ ├── annotion
│ │ │ ├── FField.java
│ │ │ ├── FFieldInput.java
│ │ │ ├── FFieldLength.java
│ │ │ ├── FFieldLimit.java
│ │ │ ├── FFieldNumberRange.java
│ │ │ ├── FFieldOptions.java
│ │ │ ├── FFieldValidator.java
│ │ │ └── Form.java
│ │ │ ├── field
│ │ │ ├── FormFieldDivider.java
│ │ │ ├── FormFieldEditText.java
│ │ │ ├── FormFieldMoneyEditText.java
│ │ │ ├── FormFieldSpinner.java
│ │ │ └── FormFieldTitle.java
│ │ │ ├── validator
│ │ │ ├── BaseValidator.java
│ │ │ ├── ExpressionValidator.java
│ │ │ ├── LengthValidator.java
│ │ │ ├── NotNullValidator.java
│ │ │ └── NumberRangeValidator.java
│ │ │ └── window
│ │ │ └── DialogSpinnerSelect.java
│ └── res
│ │ ├── drawable-hdpi
│ │ ├── bg_dialog_form_spinner.9.png
│ │ ├── bg_edit_style_1.9.png
│ │ ├── ic_pwd_eye_hide.png
│ │ └── ic_pwd_eye_visible.png
│ │ ├── drawable-xxhdpi
│ │ └── form_field_end_arrow.png
│ │ ├── drawable
│ │ ├── btn_pwd_switch.xml
│ │ ├── form_spinner_selector.xml
│ │ └── text_cursor.xml
│ │ ├── layout
│ │ ├── dialog_form_spinner.xml
│ │ ├── dialog_select_date.xml
│ │ ├── field_btn_pwd.xml
│ │ ├── field_divider.xml
│ │ ├── field_edittext.xml
│ │ ├── field_edittext_hint_title.xml
│ │ ├── field_spinner.xml
│ │ ├── field_spinner_item.xml
│ │ └── field_title.xml
│ │ └── values
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── xyz
│ └── codedream
│ └── lib
│ └── form
│ └── ExampleUnitTest.java
├── screenshot
├── result.png
└── validate.png
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | C:\Users\skg\AppData\Roaming\Subversion
48 |
49 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### 为什么开发FormLayout?
2 |
3 | 在Android中如果一个表单里有很多个表单项(FormField),甚至是多个页面都有类似的表单的需求,那么这些表单布局编写起来是很繁重的,而且需求变动,难以维护。即使编写不是问题,FormField的逐条验证,逐条提取数据绑定,也是非常头疼和难以维护的。为了解决这些问题,开发了FormLayout组建。
4 |
5 | ### FormLayout设计要求
6 | 1,支持根据model属性,自动生成FormField UI。
7 | 2,支持表单项内容合法性自动校验。
8 | 3,支持表单项从UI到model的反向赋值。
9 | 4,支持自定义验证规则。
10 | 5,支持model属性默认值配置(直接给变量赋值,即可解析为默认值)
11 |
12 | ### FormLayout的实现思路
13 | A.根据Mode生成UI
14 | 通过注解描述model字段属性,包括数据源类型,数据校验规则,默认值等,然后由FormLayout解析并绑定到FormField UI。
15 | Form表单UI支持扩展,添加,需要从FormField继承或者FormField的子类继承。
16 |
17 | B.数据验证支持
18 | 数据校验依赖于Validator链式校验借口,可以组成校验连,每个链节点支持多个子节点。
19 | Validator借口支持默认错误信息,自定义错误信息,模版错误信息。
20 |
21 | C.数据校验工作流程
22 | 当FormLayout调用validateForm();的时候,FormLayout会遍历FormField,并拿到FormField的约束规则,自动校验,如发现任何一个条件失败,则终止校验并通知失败。
23 |
24 | D.数据反填到model
25 | 在解析model属性的时候会同时获取model属性的java.lang.reflect.Field对象,在有了Field和model以后,就可以把FormField的value反填到model了。
26 |
27 | ### 常用注解
28 | 1,@Form注解标识model对象。
29 | 2,@FField描述为model里面哪个属性作为表单项,并声明数据源类型。
30 | 3,@FFieldLength(range = {2, 10})//字符长度限制
31 | 4,@FFieldOptions(EnumOptions.class)//Spinner数据源
32 | 5,@FFieldInput(type = FFieldInput.TYPE_CLASS_NUMBER)//输入类型
33 | 6,@FFieldNumberRange(range = {0.0f, 100000.0f})//数据值范围
34 |
35 |
36 | ### 代码用例
37 |
38 | ```java
39 | @Form
40 | public class MainActivity extends AppCompatActivity implements OnClickListener {
41 | @FField(id = 0, titleId = R.string.form_name)
42 | @FFieldLength(range = {2, 10})
43 | private String name;
44 |
45 | @FField(id = 1, titleId = R.string.form_gender, type = FieldType.SPINNER)
46 | @FFieldOptions(EnumOptions.class)
47 | private Gender gender;
48 |
49 | @FField(id = 2, titleId = R.string.form_job, notNull = false)
50 | @FFieldLength(range = {2, 20})
51 | private String job;
52 |
53 | @FField(id = 3, titleId = R.string.form_salary)
54 | @FFieldInput(type = FFieldInput.TYPE_CLASS_NUMBER)
55 | @FFieldNumberRange(range = {0.0f, 100000.0f})
56 | private float salary;
57 |
58 | public enum Gender {
59 | male, female;
60 | }
61 |
62 | private FormLayout mFormLayout;
63 |
64 | @Override
65 | protected void onCreate(Bundle savedInstanceState) {
66 | super.onCreate(savedInstanceState);
67 | setContentView(R.layout.activity_main);
68 | findViewById(R.id.btn_submit).setOnClickListener(this);
69 | mFormLayout = (FormLayout) findViewById(R.id.formlayout);
70 | mFormLayout.parseAndBindField(this, new ParseFieldFilter() {
71 | @Override
72 | public boolean isSkip(int id) {
73 | return false;
74 | }
75 |
76 | @Override
77 | public void onFieldCreated(int id, FormField> f) {
78 | }
79 | });
80 | }
81 |
82 | private void submit() {
83 | if (mFormLayout.validateForm()) {
84 | StringBuilder sb = new StringBuilder();
85 | sb.append("FormlayoutResult:\n\n");
86 | sb.append("Name : ").append(name).append('\n');
87 | sb.append("Gender : ").append(gender).append('\n');
88 | sb.append("Job : ").append(job).append('\n');
89 | sb.append("Salary : ").append(salary).append('\n');
90 | new AlertDialog.Builder(this)
91 | .setMessage(sb.toString())
92 | .setPositiveButton(android.R.string.ok, null)
93 | .show();
94 | }
95 | }
96 |
97 | @Override
98 | public void onClick(View v) {
99 | switch (v.getId()) {
100 | case R.id.btn_submit:
101 | submit();
102 | break;
103 | }
104 | }
105 | ```
106 | ### 效果图
107 | 
108 | 
109 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "xyz.codedream.lib.form.sample"
8 | minSdkVersion 12
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.3.1'
28 | compile 'com.android.support.constraint:constraint-layout:1.0.2'
29 | testCompile 'junit:junit:4.12'
30 | compile project(':library')
31 | }
32 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Android\Android_Studio_sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/xyz/codedream/lib/form/sample/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package xyz.codedream.lib.form.sample;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("xyz.codedream.lib.form.sample", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/xyz/codedream/lib/form/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | package xyz.codedream.lib.form.sample;
2 |
3 | import android.app.AlertDialog;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.os.Bundle;
6 | import android.view.View;
7 | import android.view.View.OnClickListener;
8 |
9 | import xyz.codedream.lib.form.FieldOptions.EnumOptions;
10 | import xyz.codedream.lib.form.FormField;
11 | import xyz.codedream.lib.form.FormLayout;
12 | import xyz.codedream.lib.form.FormLayout.ParseFieldFilter;
13 | import xyz.codedream.lib.form.annotion.FField;
14 | import xyz.codedream.lib.form.annotion.FField.FieldType;
15 | import xyz.codedream.lib.form.annotion.FFieldInput;
16 | import xyz.codedream.lib.form.annotion.FFieldLength;
17 | import xyz.codedream.lib.form.annotion.FFieldNumberRange;
18 | import xyz.codedream.lib.form.annotion.FFieldOptions;
19 | import xyz.codedream.lib.form.annotion.Form;
20 |
21 | @Form
22 | public class MainActivity extends AppCompatActivity implements OnClickListener {
23 | @FField(id = 0, titleId = R.string.form_name)
24 | @FFieldLength(range = {2, 10})
25 | private String name;
26 |
27 | @FField(id = 1, titleId = R.string.form_gender, type = FieldType.SPINNER)
28 | @FFieldOptions(EnumOptions.class)
29 | private Gender gender;
30 |
31 | @FField(id = 2, titleId = R.string.form_job, notNull = false)
32 | @FFieldLength(range = {2, 20})
33 | private String job;
34 |
35 | @FField(id = 3, titleId = R.string.form_salary)
36 | @FFieldInput(type = FFieldInput.TYPE_CLASS_NUMBER)
37 | @FFieldNumberRange(range = {0.0f, 100000.0f})
38 | private float salary;
39 |
40 | public enum Gender {
41 | male, female;
42 | }
43 |
44 | private FormLayout mFormLayout;
45 |
46 | @Override
47 | protected void onCreate(Bundle savedInstanceState) {
48 | super.onCreate(savedInstanceState);
49 | setContentView(R.layout.activity_main);
50 | findViewById(R.id.btn_submit).setOnClickListener(this);
51 | mFormLayout = (FormLayout) findViewById(R.id.formlayout);
52 | mFormLayout.parseAndBindField(this, new ParseFieldFilter() {
53 | @Override
54 | public boolean isSkip(int id) {
55 | return false;
56 | }
57 |
58 | @Override
59 | public void onFieldCreated(int id, FormField> f) {
60 | }
61 | });
62 | }
63 |
64 | private void submit() {
65 | if (mFormLayout.validateForm()) {
66 | StringBuilder sb = new StringBuilder();
67 | sb.append("FormlayoutResult:\n\n");
68 | sb.append("Name : ").append(name).append('\n');
69 | sb.append("Gender : ").append(gender).append('\n');
70 | sb.append("Job : ").append(job).append('\n');
71 | sb.append("Salary : ").append(salary).append('\n');
72 | new AlertDialog.Builder(this)
73 | .setMessage(sb.toString())
74 | .setPositiveButton(android.R.string.ok, null)
75 | .show();
76 | }
77 | }
78 |
79 | @Override
80 | public void onClick(View v) {
81 | switch (v.getId()) {
82 | case R.id.btn_submit:
83 | submit();
84 | break;
85 | }
86 | }
87 |
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
15 |
16 |
21 |
22 |
23 |
27 |
28 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | FormLayout
3 | UserName
4 | Age
5 | Gender
6 | Job
7 | Salary(CNY)
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/xyz/codedream/lib/form/sample/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package xyz.codedream.lib.form.sample;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.3.2'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1024m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shenkaige/FormLayout/6bcacad8be88cafd6221afd364ce89b336123583/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue May 16 12:52:38 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 12
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | compile fileTree(dir: 'libs', include: ['*.jar'])
26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27 | exclude group: 'com.android.support', module: 'support-annotations'
28 | })
29 | compile 'com.android.support:appcompat-v7:25.3.1'
30 | testCompile 'junit:junit:4.12'
31 | }
32 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Android\Android_Studio_sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/library/src/androidTest/java/xyz/codedream/lib/form/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package xyz.codedream.lib.form;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("xyz.codedream.lib.form.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/library/src/main/java/xyz/codedream/lib/form/AbstractFormField.java:
--------------------------------------------------------------------------------
1 | package xyz.codedream.lib.form;
2 |
3 | import java.lang.reflect.Field;
4 | import java.util.List;
5 |
6 | import xyz.codedream.lib.form.validator.BaseValidator;
7 | import xyz.codedream.lib.form.validator.ExpressionValidator;
8 | import xyz.codedream.lib.form.validator.NotNullValidator;
9 |
10 | import android.content.Context;
11 |
12 | /**
13 | * 基础实现
14 | *
15 | * @author skg
16 | *
17 | * @param
18 | */
19 | public abstract class AbstractFormField implements FormField {
20 |
21 | private final int mId;
22 | private String mTitle;
23 | private Validator mValidator;
24 | private FormLayout mForm;
25 |
26 | public AbstractFormField(int id) {
27 | mId = id;
28 | }
29 |
30 | @Override
31 | public int getId() {
32 | return mId;
33 | }
34 |
35 | protected void setTitle(String title) {
36 | mTitle = title;
37 | }
38 |
39 | @Override
40 | public String getTitle() {
41 | return mTitle;
42 | }
43 |
44 | private Field mField;
45 |
46 | @Override
47 | public void bindField(Field field) {
48 | mField = field;
49 | }
50 |
51 | @Override
52 | public Field getBindField() {
53 | return mField;
54 | }
55 |
56 | private FieldOptions mFieldOptions;
57 |
58 | @Override
59 | public void setFieldOptions(FieldOptions opt) {
60 | mFieldOptions = opt;
61 | }
62 |
63 | @Override
64 | public FieldOptions getFieldOptions() {
65 | return mFieldOptions;
66 | }
67 |
68 | @Override
69 | public AbstractFormField setNullable(boolean nullable) {
70 | if (nullable) {
71 | if (mValidator != null) {
72 | if (mValidator instanceof NotNullValidator) {
73 | List> sub = mValidator.getSubsequent();
74 | if (sub == null || sub.isEmpty()) {
75 | mValidator = null;
76 | } else {
77 | // FIXME
78 | mValidator = new BaseValidator() {
79 | @Override
80 | public boolean validate(Result result) {
81 | return true;
82 | }
83 |
84 | @Override
85 | public String getErrorMessage(Context context, String fieldTitle) {
86 | return null;
87 | }
88 | };
89 | for (Validator v : sub) {
90 | mValidator.addSubsequent(v);
91 | }
92 | }
93 | }
94 | }
95 | } else {
96 | // FIXME
97 | }
98 | return this;
99 | }
100 |
101 | @Override
102 | public boolean isNullable() {
103 | if (mValidator == null) {
104 | return true;
105 | }
106 | return mValidator.findByClass(NotNullValidator.class) == null;
107 | }
108 |
109 | @Override
110 | public AbstractFormField setValidator(String expression, String errorMessage) {
111 | ExpressionValidator v = new ExpressionValidator();
112 | v.setErrorTemplate(errorMessage, false);
113 | mValidator = v;
114 | return this;
115 | }
116 |
117 | @Override
118 | public AbstractFormField setValidator(Validator validator) {
119 | mValidator = validator;
120 | return this;
121 | }
122 |
123 | @Override
124 | public boolean validateResult() {
125 | final String title = getTitle();
126 | if (mValidator == null) {
127 | return true;
128 | }
129 | return validate(title, getResult(), mValidator);
130 | }
131 |
132 | private boolean validate(String title, Result result, Validator v) {
133 | if (v.validate(result)) {
134 | List> sub = v.getSubsequent();
135 | if (sub == null || sub.isEmpty()) {
136 | return true;
137 | } else {
138 | for (Validator nv : sub) {
139 | boolean r = validate(title, result, nv);
140 | if (!r) {
141 | mForm.tipsError(nv.getErrorMessage(mForm.getContext(), title));
142 | return false;
143 | }
144 | }
145 | return true;
146 | }
147 | } else {
148 | mForm.tipsError(v.getErrorMessage(mForm.getContext(), title));
149 | return false;
150 | }
151 | }
152 |
153 | @Override
154 | public void onFormAttached(FormLayout form) {
155 | mForm = form;
156 | }
157 |
158 | @Override
159 | public FormLayout getForm() {
160 | return mForm;
161 | }
162 |
163 | public boolean isEmptyString(Result result) {
164 | if (result instanceof String) {
165 | String str = result.toString();
166 | str = str.trim();
167 | return str.length() <= 0;
168 | } else {
169 | return false;
170 | }
171 | }
172 |
173 | }
174 |
--------------------------------------------------------------------------------
/library/src/main/java/xyz/codedream/lib/form/BaseViewFormField.java:
--------------------------------------------------------------------------------
1 | package xyz.codedream.lib.form;
2 |
3 | import android.content.Context;
4 | import android.graphics.drawable.Drawable;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.widget.EditText;
8 | import android.widget.TextView;
9 |
10 | /**
11 | * 表单View基础实现
12 | *
13 | * @author skg
14 | *
15 | * @param
16 | */
17 | public abstract class BaseViewFormField extends AbstractFormField {
18 |
19 | private final int mTitleViewId;
20 |
21 | public BaseViewFormField(int id) {
22 | this(id, R.id.field_title);
23 | }
24 |
25 | public BaseViewFormField(int id, int titleViewId) {
26 | super(id);
27 | mTitleViewId = titleViewId;
28 | }
29 |
30 | @Override
31 | public final void onFormAttached(FormLayout form) {
32 | super.onFormAttached(form);
33 | onCreateView(form.getContext());
34 | }
35 |
36 | protected abstract void onCreateView(Context context);
37 |
38 | private View mContentView;
39 | private TextView mTitleView;
40 |
41 | @Override
42 | public View getShowView() {
43 | return mContentView;
44 | }
45 |
46 | public void setContentView(int layoutId) {
47 | setContentView(LayoutInflater.from(getContent()).inflate(layoutId, null, false));
48 | }
49 |
50 | public void setContentView(View view) {
51 | mContentView = view;
52 | View titleView = mContentView.findViewById(mTitleViewId);
53 | if (titleView instanceof TextView) {
54 | mTitleView = (TextView) titleView;
55 | }
56 | refreshTitle();
57 | applyIconInner();
58 | }
59 |
60 | protected void refreshTitle() {
61 | if (mTitleView != null) {
62 | if (mTitleView instanceof EditText) {
63 | mTitleView.setHint(getTitle());
64 | } else {
65 | mTitleView.setText(getTitle());
66 | }
67 | checkSetTitleColor();
68 | }
69 | }
70 |
71 | @SuppressWarnings("unchecked")
72 | protected T findViewById(int id) {
73 | return (T) mContentView.findViewById(id);
74 | }
75 |
76 | @Override
77 | protected void setTitle(String title) {
78 | super.setTitle(title);
79 | refreshTitle();
80 | }
81 |
82 | private Context getContent() {
83 | return getForm().getContext();
84 | }
85 |
86 | private Integer mTitleColor;
87 |
88 | @Override
89 | public BaseViewFormField setTitleColor(int color) {
90 | mTitleColor = color;
91 | checkSetTitleColor();
92 | return this;
93 | }
94 |
95 | private void checkSetTitleColor() {
96 | if (mTitleColor != null && mTitleView != null) {
97 | if (mTitleView instanceof EditText) {
98 | mTitleView.setHintTextColor(mTitleColor);
99 | } else {
100 | mTitleView.setTextColor(mTitleColor);
101 | }
102 | }
103 | }
104 |
105 | private Drawable mIconDrawable;
106 | private int mIconMargin;
107 |
108 | @Override
109 | public BaseViewFormField setIcon(Drawable icon, int margin) {
110 | mIconDrawable = icon;
111 | mIconMargin = margin;
112 | applyIconInner();
113 | return this;
114 | }
115 |
116 | private void applyIconInner() {
117 | if (mTitleView != null) {
118 | Drawable[] dd = mTitleView.getCompoundDrawables();
119 | mTitleView.setCompoundDrawablesWithIntrinsicBounds(//
120 | mIconDrawable, //
121 | dd[1], //
122 | dd[2], //
123 | dd[3]);
124 | mTitleView.setCompoundDrawablePadding(mIconMargin);
125 | }
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/library/src/main/java/xyz/codedream/lib/form/FieldOptions.java:
--------------------------------------------------------------------------------
1 | package xyz.codedream.lib.form;
2 |
3 | import android.content.Context;
4 |
5 | public interface FieldOptions {
6 |
7 | public int getCount(Class> fieldType);
8 |
9 | public String[] getShowArray(Class> fieldType, Context context);
10 |
11 | public T[] getValues(Class> fieldType, Context context);
12 |
13 | public T getValue(Class> fieldType, int position);
14 |
15 | public static class EnumOptions implements FieldOptions> {
16 |
17 | @Override
18 | public int getCount(Class> fieldType) {
19 | if (fieldType.isEnum()) {
20 | return fieldType.getEnumConstants().length;
21 | } else {
22 | return 0;
23 | }
24 | }
25 |
26 | @Override
27 | public String[] getShowArray(Class> fieldType, Context context) {
28 | Object[] enumValues = getValues(fieldType, context);
29 | if (enumValues == null || enumValues.length <= 0) {
30 | return null;
31 | } else {
32 | int size = enumValues.length;
33 | String[] stringValues = new String[size];
34 | for (int i = 0; i < size; i++) {
35 | stringValues[i] = enumValues[i].toString();
36 | }
37 | return stringValues;
38 | }
39 | }
40 |
41 | @Override
42 | public Enum> getValue(Class> fieldType, int position) {
43 | if (fieldType.isEnum()) {
44 | return (Enum>) fieldType.getEnumConstants()[position];
45 | } else {
46 | return null;
47 | }
48 | }
49 |
50 | @Override
51 | public Enum>[] getValues(Class> fieldType, Context context) {
52 | if (fieldType.isEnum()) {
53 | return (Enum>[]) fieldType.getEnumConstants();
54 | } else {
55 | return null;
56 | }
57 | }
58 |
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/library/src/main/java/xyz/codedream/lib/form/FieldResultWriter.java:
--------------------------------------------------------------------------------
1 | package xyz.codedream.lib.form;
2 |
3 | import java.lang.reflect.Field;
4 |
5 | public class FieldResultWriter {
6 |
7 | private FieldResultWriter() {
8 | }
9 |
10 | public static void write(Field field, Object target, Object formFieldvalue) {
11 | if (target != null && field != null) {
12 | field.setAccessible(true);
13 | Class> fieldType = field.getType();
14 | if (fieldType.isPrimitive()) {
15 | handlerPrimitive(field, fieldType, target, formFieldvalue);
16 | } else if (Number.class.isAssignableFrom(fieldType)) {
17 | handlerNumber(field, fieldType, target, formFieldvalue);
18 | } else {
19 | try {
20 | if (formFieldvalue == null || fieldType.isAssignableFrom(formFieldvalue.getClass())) {
21 | field.set(target, formFieldvalue);
22 | } else {
23 | // resolve real type data handler
24 | }
25 | } catch (IllegalAccessException e) {
26 | e.printStackTrace();
27 | } catch (IllegalArgumentException e) {
28 | e.printStackTrace();
29 | }
30 | }
31 | }
32 | }
33 |
34 | private static void handlerNumber(Field field, Class> fieldType, Object target, Object formFieldvalue) {
35 | try {
36 | if (formFieldvalue instanceof Number) {
37 | field.set(target, formFieldvalue);
38 | } else if (formFieldvalue == null) {
39 | return;
40 | } else {
41 | final String parseStr = formFieldvalue.toString();
42 | if (Integer.class == fieldType) {
43 | field.set(target, Integer.parseInt(parseStr));
44 | } else if (Float.class == fieldType) {
45 | field.set(target, Float.parseFloat(parseStr));
46 | } else if (Long.class == fieldType) {
47 | field.set(target, Long.parseLong(parseStr));
48 | } else if (Double.class == fieldType) {
49 | field.set(target, Double.parseDouble(parseStr));
50 | } else if (Byte.class == fieldType) {
51 | field.set(target, Byte.parseByte(parseStr));
52 | } else if (Short.class == fieldType) {
53 | field.set(target, Short.parseShort(parseStr));
54 | } else {
55 | throw new IllegalArgumentException(
56 | "unsupport argument number type:" + fieldType.getCanonicalName());
57 | }
58 | }
59 | } catch (IllegalAccessException e) {
60 | e.printStackTrace();
61 | } catch (IllegalArgumentException e) {
62 | e.printStackTrace();
63 | }
64 | }
65 |
66 | private static void handlerPrimitive(Field field, Class> fieldType, Object target, Object value) {
67 | if (value == null) {
68 | // ignore
69 | return;
70 | }
71 | try {
72 | final String parseStr = value.toString();
73 | if (int.class == fieldType) {
74 | field.setInt(target, Integer.parseInt(parseStr));
75 | } else if (float.class == fieldType) {
76 | field.setFloat(target, Float.parseFloat(parseStr));
77 | } else if (long.class == fieldType) {
78 | field.setLong(target, Long.parseLong(parseStr));
79 | } else if (double.class == fieldType) {
80 | field.setDouble(target, Double.parseDouble(parseStr));
81 | } else if (byte.class == fieldType) {
82 | field.setByte(target, Byte.parseByte(parseStr));
83 | } else if (short.class == fieldType) {
84 | field.setShort(target, Short.parseShort(parseStr));
85 | } else {
86 | throw new IllegalArgumentException("unsupport argument primitive type:" + fieldType.getCanonicalName());
87 | }
88 | } catch (IllegalAccessException e) {
89 | e.printStackTrace();
90 | } catch (IllegalArgumentException e) {
91 | e.printStackTrace();
92 | }
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/library/src/main/java/xyz/codedream/lib/form/FormField.java:
--------------------------------------------------------------------------------
1 | package xyz.codedream.lib.form;
2 |
3 | import java.lang.reflect.Field;
4 |
5 | import android.graphics.drawable.Drawable;
6 | import android.view.View;
7 |
8 | /**
9 | * 筛选的类别
10 | *
11 | * @author sky
12 | *
13 | */
14 | public interface FormField {
15 |
16 | /**
17 | * this field just show
18 | *
19 | * @author skg
20 | *
21 | */
22 | public interface JustShowField {
23 | }
24 |
25 | public FormLayout getForm();
26 |
27 | public void bindField(Field field);
28 |
29 | public Field getBindField();
30 |
31 | public void setFieldOptions(FieldOptions opt);
32 |
33 | public FieldOptions getFieldOptions();
34 |
35 | public void onFormAttached(FormLayout form);
36 |
37 | /**
38 | * 获取Id
39 | *
40 | * @return
41 | */
42 | public int getId();
43 |
44 | /**
45 | * 获取选择类别名称
46 | *
47 | * @return
48 | */
49 | public String getTitle();
50 |
51 | /**
52 | * 根据Index获取选择的结果
53 | *
54 | * @return
55 | */
56 | public Result getResult();
57 |
58 | public FormField resetResult();
59 |
60 | public View getShowView();
61 |
62 | public FormField setTitleColor(int color);
63 |
64 | public FormField setIcon(Drawable icon, int margin);
65 |
66 | public FormField setNullable(boolean nullable);
67 |
68 | public boolean isNullable();
69 |
70 | public FormField setValidator(String expression, String errorMessage);
71 |
72 | public FormField setValidator(Validator validator);
73 |
74 | public boolean validateResult();
75 |
76 | public void setDefaultValue(Result value);
77 |
78 | public enum FieldInputType {
79 | TEXT, NUMBER
80 | }
81 |
82 | public enum PasswordStyle {
83 | /** Not password */
84 | NONE,
85 | /** Visible password */
86 | VISIBLE,
87 | /** Hide password */
88 | HIDE
89 | }
90 | }
--------------------------------------------------------------------------------
/library/src/main/java/xyz/codedream/lib/form/FormLayout.java:
--------------------------------------------------------------------------------
1 | package xyz.codedream.lib.form;
2 |
3 | import android.content.Context;
4 | import android.text.TextUtils;
5 | import android.util.AttributeSet;
6 | import android.util.Log;
7 | import android.util.SparseArray;
8 | import android.view.View;
9 | import android.widget.LinearLayout;
10 | import android.widget.Toast;
11 |
12 | import java.lang.reflect.Field;
13 | import java.util.ArrayList;
14 | import java.util.Collections;
15 | import java.util.Comparator;
16 | import java.util.List;
17 |
18 | import xyz.codedream.lib.form.FormField.JustShowField;
19 | import xyz.codedream.lib.form.FormField.PasswordStyle;
20 | import xyz.codedream.lib.form.annotion.FField;
21 | import xyz.codedream.lib.form.annotion.FFieldInput;
22 | import xyz.codedream.lib.form.annotion.FFieldLength;
23 | import xyz.codedream.lib.form.annotion.FFieldNumberRange;
24 | import xyz.codedream.lib.form.annotion.FFieldOptions;
25 | import xyz.codedream.lib.form.annotion.FFieldValidator;
26 | import xyz.codedream.lib.form.annotion.Form;
27 | import xyz.codedream.lib.form.field.FormFieldDivider;
28 | import xyz.codedream.lib.form.field.FormFieldEditText;
29 | import xyz.codedream.lib.form.field.FormFieldMoneyEditText;
30 | import xyz.codedream.lib.form.field.FormFieldSpinner;
31 | import xyz.codedream.lib.form.field.FormFieldTitle;
32 | import xyz.codedream.lib.form.validator.LengthValidator;
33 | import xyz.codedream.lib.form.validator.NotNullValidator;
34 | import xyz.codedream.lib.form.validator.NumberRangeValidator;
35 |
36 | /**
37 | * Form controller
38 | *
39 | * @author skg
40 | *
41 | */
42 | public class FormLayout extends LinearLayout {
43 | private static final String TAG = FormLayout.class.getSimpleName();
44 | private SparseArray> mFormFieldList = new SparseArray>();
45 |
46 | public FormLayout(Context context, AttributeSet attrs) {
47 | super(context, attrs);
48 | init();
49 | }
50 |
51 | public FormLayout(Context context) {
52 | super(context);
53 | init();
54 | }
55 |
56 | private void init() {
57 | setOrientation(VERTICAL);
58 | }
59 |
60 | public void tipsError(String msg) {
61 | Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
62 | }
63 |
64 | /**
65 | * Add money edit
66 | */
67 | public FormFieldMoneyEditText money(int titleResId) {
68 | return money(titleResId, false);
69 | }
70 |
71 | /**
72 | * Add money edit
73 | */
74 | public FormFieldMoneyEditText money(int titleResId, boolean titleInHint) {
75 | FormFieldMoneyEditText field = new FormFieldMoneyEditText(titleResId, getString(titleResId), titleInHint);
76 | addField(field);
77 | return field;
78 | }
79 |
80 | /**
81 | * 添加输入框
82 | */
83 | public FormFieldEditText editPwd(int titleResId, boolean titleInHint) {
84 | return edit(titleResId, titleInHint, PasswordStyle.HIDE);
85 | }
86 |
87 | /**
88 | * 添加输入框
89 | */
90 | public FormFieldEditText edit(int titleResId) {
91 | return edit(titleResId, false);
92 | }
93 |
94 | /**
95 | * 添加输入框
96 | */
97 | public FormFieldEditText edit(int titleResId, boolean titleInHint) {
98 | return edit(titleResId, titleInHint, PasswordStyle.NONE);
99 | }
100 |
101 | /**
102 | * 添加输入框
103 | *
104 | * @param titleResId
105 | * @param titleInHint
106 | * @return
107 | */
108 | public FormFieldEditText edit(int titleResId, boolean titleInHint, PasswordStyle pwdStyle) {
109 | FormFieldEditText field = new FormFieldEditText(titleResId, getString(titleResId), titleInHint, pwdStyle);
110 | addField(field);
111 | return field;
112 | }
113 |
114 | /**
115 | * 添加选择器
116 | *
117 | * @param titleResId
118 | * @param data
119 | * @return
120 | */
121 | public FormFieldSpinner spinner(int titleResId, List