前言
在开发中,我们常常遇到这种情况
我们往往需要的是下面这种效果
但是如果把这些实现的代码写在Activity中会比较麻烦,影响代码美观
解决方法
于是就有了下面这个辅助类,禁用了按钮的点击事件和按钮的渐变色,可同时添加一个或者多个EditText
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/** * 文本输入辅助类,通过管理多个TextView或者EditText输入是否为空来启用或者禁用按钮的点击事件 */ public final class TextInputHelper implements TextWatcher { private View mMainView; //操作按钮的View private List<TextView> mViewSet; //TextView集合,子类也可以(EditText、TextView、Button) private boolean isAlpha; //是否设置透明度 public TextInputHelper(View view) { this (view, true ); } /** * 构造函数 * * @param view 跟随EditText或者TextView输入为空来判断启动或者禁用这个View * @param alpha 是否需要设置透明度 */ public TextInputHelper(View view, boolean alpha) { if (view == null ) throw new IllegalArgumentException( "The view is empty" ); mMainView = view; isAlpha = alpha; } /** * 添加EditText或者TextView监听 * * @param views 传入单个或者多个EditText或者TextView对象 */ public void addViews(TextView... views) { if (views == null ) return ; if (mViewSet == null ) { mViewSet = new ArrayList<>(views.length - 1 ); } for (TextView view : views) { view.addTextChangedListener( this ); mViewSet.add(view); } afterTextChanged( null ); } /** * 移除EditText监听,避免内存泄露 */ public void removeViews() { if (mViewSet == null ) return ; for (TextView view : mViewSet) { view.removeTextChangedListener( this ); } mViewSet.clear(); mViewSet = null ; } // TextWatcher @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public synchronized void afterTextChanged(Editable s) { if (mViewSet == null ) return ; for (TextView view : mViewSet) { if ( "" .equals(view.getText().toString())) { setEnabled( false ); return ; } } setEnabled( true ); } /** * 设置View的事件 * * @param enabled 启用或者禁用View的事件 */ public void setEnabled( boolean enabled) { if (enabled == mMainView.isEnabled()) return ; if (enabled) { //启用View的事件 mMainView.setEnabled( true ); if (isAlpha) { //设置不透明 mMainView.setAlpha(1f); } } else { //禁用View的事件 mMainView.setEnabled( false ); if (isAlpha) { //设置半透明 mMainView.setAlpha( 0 .5f); } } } } |
在Activity创建时添加监听
1
2
3
4
5
6
7
8
9
10
11
|
private TextInputHelper mInputHelper; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); //创建一个辅助类,传入按钮操作View mInputHelper = new TextInputHelper(mButton); //可添加一个或者多个EditText,当然也可以添加TextView mInputHelper.addViews(mEditText1, mEditText2, mEditText3); } |
在Activity销毁时移除监听(避免内存泄露)
1
2
3
4
5
6
7
|
@Override protected void onDestroy() { super .onDestroy(); //移除引用,避免内存泄露 mInputHelper.removeViews(); } |
需要注意的是这里不单单只是可以添加EditText,还可以添加TextView,因为EditText是TextView的子类,最后要是觉得好用记得点个赞。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:https://www.jianshu.com/p/affbd657634b