您的位置:首页 > 移动开发 > Android开发

Android控件状态依赖框架

2017-03-25 14:17 169 查看
在生产型Android客户端软件(企业级应用)开发中,界面可能存在多个输入(
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: