Android控件状态依赖框架
2017-03-25 14:17
169 查看
在生产型Android客户端软件(企业级应用)开发中,界面可能存在多个输入(
设定图中
确认操作依赖于商品编码和储位的状态
跳过操作不依赖于输入状态
登记差异操作依赖于储位和数量的状态
输入框有三种状态:
待输入;
待校验;
校验成功。
操作需要当其依赖的输入数据校验成功,才能执行。
如果在Activity中去判断输入框状态,那么实际需要调用(3个输入框)
通过对这种场景的抽象,实现了Android控件状态依赖框架,其使用方法如下:
使用方法:
布局文件引用
由于
在
直接执行
可以看出,这里并没有通过
在
在
以上,博客园对markdown支持的不太好,无法添加注释(/* */),如需查看源码,请移步Github地址:https://github.com/yhthu/AndroidViewDependency.git
EditText)和多个操作(
MotionEvent和
KeyEvent),且操作依赖于输入的状态。如下图所示的场景:
设定图中
确认操作依赖于商品编码和储位的状态
跳过操作不依赖于输入状态
登记差异操作依赖于储位和数量的状态
输入框有三种状态:
待输入;
待校验;
校验成功。
操作需要当其依赖的输入数据校验成功,才能执行。
如果在Activity中去判断输入框状态,那么实际需要调用(3个输入框)
*(3种状态)
*(3个按钮) = 27个 if 判断,对于状态的维护将使得整个程序可维护性极差,并随着输入和操作的增加,维护的状态呈指数增长。
通过对这种场景的抽象,实现了Android控件状态依赖框架,其使用方法如下:
使用方法:
布局文件引用
WatchEditText和
WatchButton
<com.android.yhthu.viewdependency.view.WatchEditText android:id="@+id/edit_query_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:tag="editQuery1" android:imeOptions="actionNext" android:hint="商品编码" android:inputType="number"/>
<com.android.yhthu.viewdependency.view.WatchButton android:id="@+id/search_button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:tag="buttonSearch1" android:text="确认" />
由于
Library Module中的控件
id不是常量(可参考
ButterKnife对
Library Module的支持采用
R2的原因),这里采用了
tag的方式。
在
Activity中通过注解申明依赖
@ViewName("商品编码") private WatchEditText editQuery1; @ViewName("储位") private WatchEditText editQuery2; @ViewName("数量") private WatchEditText editQuery3; @ViewDependency(name = @ViewName("确认"), dependency = {"editQuery1", "editQuery2"}) private WatchButton buttonSearch1; @ViewDependency(name = @ViewName("跳过")/*不依赖输入*/) private WatchButton buttonSearch2; @ViewDependency(name = @ViewName("登记缺货"), dependency = {"editQuery2", "editQuery3"}) private WatchButton buttonSearch3;
ViewName定义控件名称,
ViewDependency中
dependency指定其依赖的控件
tag。
直接执行
onClick和
onEditorAction(修改状态)
@Override public void onClick(View v) { if (v == buttonSearch1) { Toast.makeText(this, "调接口", Toast.LENGTH_SHORT).show(); } else if (v == buttonSearch2) { Toast.makeText(this, "跳下一页", Toast.LENGTH_SHORT).show(); } else if (v == buttonSearch3) { Toast.makeText(this, "登记缺货", Toast.LENGTH_SHORT).show(); } }
可以看出,这里并没有通过
if判断各个输入控件的状态。
@Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_NEXT && v == editQuery1 && (query1Str = editQuery1.getText().toString()).isEmpty()) { if (query1Str.equals("12345")) { editQuery1.complete(); return true; } } // 省略代码 return false; }
onEditorAction模拟调用软件的
Enter进行校验,这里需要注意通过
editQuery1.complete()修改该
EidtText的状态。
实现原理
整个框架分为三个package:annotation、
state和
view。
在
annotation中定义
ViewName和
ViewDependency注解,分别用于
WatchEditText和
WatchButton。
ViewName指定
WatchEditText控件在业务中的名称,
ViewDependency指定
WatchButton依赖的
WatchEditText控件;
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ViewDependency { ViewName name() default @ViewName; String[] dependency() default {}; }
在
state中通过状态模式定义
Enter、
Verify、
Complete,其基类为抽象类
Operator,定义方法
operator;
public abstract class Operator { // 操作对应的上下文 protected Context context; // 操作 public abstract boolean operator(String operatorName, String viewName); }
public class Enter extends Operator { private static Enter enter; private Enter(Context context) { this.context = context; } public static Enter getInstance(Context context) { if (enter == null) { enter = new Enter(context); } return enter; } @Override public boolean operator(String operatorName, String viewName) { Toast.makeText(context, String.format("[%s]为空,不允许执行[%s]", viewName, operatorName), Toast.LENGTH_SHORT).show(); return false; } }
WatchEditText和
WatchButton定义控件的依赖关系。
WatchEditText实现
ViewState接口,其包含三种状态的转换方法。
public interface ViewState { void enter(); void verify(); void complete(); }
以上,博客园对markdown支持的不太好,无法添加注释(/* */),如需查看源码,请移步Github地址:https://github.com/yhthu/AndroidViewDependency.git
相关文章推荐
- 详解Android控件状态依赖框架
- Android控件状态依赖框架
- Android控件状态依赖
- android使用篇(四) 注解依赖注入IOC实现绑定控件
- Android 依赖注入(DI)框架 Dagger
- android的控件框架
- 从零开始--系统深入学习android(实践-让我们开始写代码-Android框架学习-2. 输入控件)
- 不依赖任何js框架的日期控件My97 DatePicker——支持小时、分钟、秒钟
- Android Dagger依赖注入框架浅析
- Dagger——Android上的依赖注入框架
- 状态栏框架-- 深入Android应用开发:核心技术解析与最佳实践
- ThinkAndroid框架 网络状态监听原理
- Android-Color State List用于控件在各状态下的文本颜色显示
- Android ListView中子控件的状态保存以及点击子控件改变子控件状态
- 2014-05-08android使用篇(四) 注解依赖注入IOC实现绑定控件
- 减少你的代码量--Android平台依赖注入框架RoboGuice
- Android-接受来自Appwidget的广播、更新Appwidget控件的状态
- Android-接受来自Appwidget的广播、更新Appwidget控件的状态
- 【Android 开发】:UI控件之开关状态按钮 ToggleButton 的使用方法
- android使用篇(四) 注解依赖注入IOC实现绑定控件