前言
在app的输入框中,需要应用到很多带有前缀说明的输入框,运用原有的输入框和文本控件,一个带头部的输入框就会增加三个控件在layout文件中。当布局文件输入框较少的情况下,这样对后期维护影响不大,但在多个带头部的输入框下,布局文件代码量会很大,影响阅读以及后期维护。而封装过后的控件,在使用中仅仅需要几行代码可实现几十行的效果。
简介
- 带头部文字的输入框
- 可在xml定义头部文字样式
- 可在xml定义输入框样式
- 可在xml定义提示文字样式
- 可在xml定义头部和输入框的间距和边距
效果图
使用方法
复制代码
1
2
3
4
5
6
7
8<com.momin.common.widget.EditInputView android:layout_width="match_parent" android:layout_height="50dp" app:inputMarginStart="10dp" app:headerText="姓名" app:hint="请输入联系人姓名" app:inputType="text" app:maxLength="30"/>
源码在这
有帮助请点个赞
attrs.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
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<!-- 公共属性 --> <!-- 前置文字内容 --> <attr name="headerText" format="string"/> <!-- 前置文字大小 --> <attr name="headerTextSize" format="dimension"/> <!-- 前置文字大小 --> <attr name="headerTextStyle"> <flag name="normal" value="0" /> <flag name="bold" value="1" /> <flag name="italic" value="2" /> </attr> <!-- 前置文字颜色 --> <attr name="headerTextColor" format="reference|color"/> <!-- 前置文字左边间距 --> <attr name="headerPaddingStart" format="dimension"/> <!-- 前置文字右边间距 --> <attr name="headerPaddingEnd" format="dimension"/> <!-- 前置文字顶部间距 --> <attr name="headerPaddingTop" format="dimension"/> <!-- 前置文字底部间距 --> <attr name="headerPaddingBottom" format="dimension"/> <!-- 公共属性 --> <!-- 带前置文字的输入框 --> <declare-styleable name="EditInputView"> <!-- 文字内容 --> <attr name="text" format="string"/> <!-- 文字大小 --> <attr name="textSize" format="dimension"/> <!-- 文字颜色 --> <attr name="textColor" format="reference|color"/> <!-- 最大输入字符数 --> <attr name="maxLength" format="integer"/> <!-- 输入限制 --> <attr name="android:enabled"/> <!-- 输入类型 --> <attr name="android:inputType"/> <!-- 输入开始边距 --> <attr name="inputMarginStart" format="dimension"/> <!-- 输入结束边距 --> <attr name="inputMarginEnd" format="dimension"/> <!-- 输入顶部边距 --> <attr name="inputMarginTop" format="dimension"/> <!-- 输入底部边距 --> <attr name="inputMarginBottom" format="dimension"/> <!-- 输入开始间距 --> <attr name="inputPaddingStart" format="dimension"/> <!-- 输入结束间距 --> <attr name="inputPaddingEnd" format="dimension"/> <!-- 输入顶部间距 --> <attr name="inputPaddingTop" format="dimension"/> <!-- 输入底部间距 --> <attr name="inputPaddingBottom" format="dimension"/> <!-- 输入底部间距 --> <attr name="android:gravity"/> <!-- 提示文字 --> <attr name="hint" format="string"/> <!-- 提示文字颜色 --> <attr name="hintColor" format="reference|color"/> <!-- 前置文字内容 --> <attr name="headerText"/> <!-- 前置文字大小 --> <attr name="headerTextSize"/> <!-- 前置文字大小 --> <attr name="headerTextStyle"/> <!-- 前置文字颜色 --> <attr name="headerTextColor"/> <!-- 前置文字左边间距 --> <attr name="headerPaddingStart"/> <!-- 前置文字右边间距 --> <attr name="headerPaddingEnd"/> <!-- 前置文字顶部间距 --> <attr name="headerPaddingTop"/> <!-- 前置文字底部间距 --> <attr name="headerPaddingBottom"/> </declare-styleable>
common_edit_input_view.xml 布局文件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 头部文字 --> <TextView android:id="@+id/tv_edit_head" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="start|center_vertical"/> <!-- 输入框 --> <EditText android:id="@+id/et_edit_input" android:layout_toEndOf="@id/tv_edit_head" android:layout_width="match_parent" android:layout_height="match_parent" android:singleLine="true" android:background="@null" android:textColor="@color/c_2B303C" android:gravity="end|center_vertical"/> </RelativeLayout>
EditInputView.java 控件类
复制代码
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343package com.momin.common.widget; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Typeface; import android.text.InputFilter; import android.text.TextUtils; import android.util.AttributeSet; import android.view.Gravity; import android.view.LayoutInflater; import android.view.inputmethod.EditorInfo; import android.widget.EditText; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.annotation.ColorRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; import com.momin.common.R; /** * <p>Title: EditInputView</p> * <p>Description: 带头部输入框 </p> * <p>Copyright: </p> * <p>Company: </p> * * @author Momin * @version 1.0 * @date 2021/3/10 18:00 */ public class EditInputView extends RelativeLayout { TextView tvHead; EditText etInput; public EditInputView(Context context) { super(context); init(context, null); } public EditInputView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context, attrs); } /** * 初始化 * * @param context 上下文 * @param attrs 资源 */ private void init(Context context, AttributeSet attrs) { // 初始化对象 initView(context); // 获取资源对象 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EditInputView); // 初始化输入框 initEdit(context, typedArray); // 初始化头部文字 CharSequence headText = typedArray.getText(R.styleable.EditInputView_headerText); if (TextUtils.isEmpty(headText)) { // 头部为空时 tvHead.setVisibility(GONE); } else { // 头部不为空时 tvHead.setVisibility(VISIBLE); initHeaderText(context, typedArray, headText); } // 回收资源对象 typedArray.recycle(); } /** * 初始化视图 * * @param context 上下文 */ private void initView(Context context) { LayoutInflater.from(context).inflate(R.layout.common_edit_input_view, this); tvHead = findViewById(R.id.tv_edit_head); etInput = findViewById(R.id.et_edit_input); } /** * 初始化输入框 * * @param context 上下文 * @param typedArray 资源对象 */ private void initEdit(Context context, TypedArray typedArray) { // 初始内容 CharSequence editText = typedArray.getText(R.styleable.EditInputView_text); if (!TextUtils.isEmpty(editText)) { etInput.setText(editText); } // 字体大小 setViewTextSize(etInput, R.styleable.EditInputView_textSize, typedArray); // 字体颜色 setViewTextColor(context, etInput, R.styleable.EditInputView_textColor, typedArray); // 设置间距 setEditPadding(typedArray); // 设置边距 setEditMargin(typedArray); // 输入类型限制 setLimitInputType(typedArray); // 输入长度限制 setLimitInputLen(typedArray); // 输入限制:可输入性 setInputBoolean(typedArray); // 输入字体排列位置 setInputGravity(typedArray); initEditHint(context, typedArray); } /** * 设置字体大小 * * @param view 被设置对象 * @param attrId 属性Id * @param typedArray 资源对象 */ private void setViewTextSize(TextView view, int attrId, TypedArray typedArray) { float size = typedArray.getDimension(attrId, 14 * view.getPaint().density); view.getPaint().setTextSize(size); } /** * 设置字体风格 * * @param view 被设置对象 * @param attrId 属性Id * @param typedArray 资源对象 */ private void setViewTextStyle(TextView view, int attrId, TypedArray typedArray) { int style = typedArray.getInt(attrId, Typeface.NORMAL); view.setTypeface(Typeface.defaultFromStyle(style)); } /** * 设置字体颜色 * * @param context 上下文 * @param view 被设置对象 * @param attrId 属性Id * @param typedArray 资源对象 */ private void setViewTextColor(Context context, TextView view, int attrId, TypedArray typedArray) { int color = typedArray.getColor(attrId, ContextCompat.getColor(context, R.color.c_2B303C)); view.setTextColor(color); } /** * 设置输入框间距 * * @param typedArray 资源对象 */ private void setEditPadding(TypedArray typedArray) { // 开始间距 int paddingStart = (int)typedArray.getDimension(R.styleable.EditInputView_inputPaddingStart, 0); // 结束间距 int paddingEnd = (int)typedArray.getDimension(R.styleable.EditInputView_inputPaddingEnd, 0); // 顶部间距 int paddingTop = (int)typedArray.getDimension(R.styleable.EditInputView_inputPaddingTop, 0); // 底部间距 int paddingBottom = (int)typedArray.getDimension(R.styleable.EditInputView_inputPaddingBottom, 0); etInput.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom); } /** * 设置输入框边距 * * @param typedArray 资源对象 */ private void setEditMargin(TypedArray typedArray) { // 开始边距 int marginStart = (int)typedArray.getDimension(R.styleable.EditInputView_inputMarginStart, 0); // 结束边距 int marginEnd = (int)typedArray.getDimension(R.styleable.EditInputView_inputMarginEnd, 0); // 顶部边距 int marginTop = (int)typedArray.getDimension(R.styleable.EditInputView_inputMarginTop, 0); // 底部边距 int marginBottom = (int)typedArray.getDimension(R.styleable.EditInputView_inputMarginBottom, 0); LayoutParams layoutParams = (LayoutParams)etInput.getLayoutParams(); layoutParams.setMargins(marginStart, marginTop, marginEnd, marginBottom); etInput.setLayoutParams(layoutParams); } /** * 设置输入类型限制 * * @param typedArray 资源对象 */ private void setLimitInputType(TypedArray typedArray) { etInput.setInputType(typedArray.getInt(R.styleable.EditInputView_android_inputType, EditorInfo.TYPE_NULL)); } /** * 设置输入长度限制 * * @param typedArray 资源对象 */ private void setLimitInputLen(TypedArray typedArray) { int len = typedArray.getInteger(R.styleable.EditInputView_maxLength, 0); if (len > 0) { setMaxLength(len); } } /** * 输入限制:可输入性 * * @param typedArray 资源对象 */ private void setInputBoolean(TypedArray typedArray) { etInput.setEnabled(typedArray.getBoolean(R.styleable.EditInputView_android_enabled, true)); } /** * 输入字体排列位置 * * @param typedArray 资源对象 */ private void setInputGravity(TypedArray typedArray) { etInput.setGravity(typedArray.getInt(R.styleable.EditInputView_android_gravity, Gravity.END|Gravity.CENTER_VERTICAL)); } /** * 初始化输入框提示文字 * * @param context 上上下文 * @param typedArray 资源对象 */ private void initEditHint(Context context, TypedArray typedArray) { CharSequence hintText = typedArray.getText(R.styleable.EditInputView_hint); if (!TextUtils.isEmpty(hintText)) { // 提示文字不为空 // 提示内容 etInput.setHint(hintText); // 提示文字颜色 int color = typedArray.getColor(R.styleable.EditInputView_hintColor, ContextCompat.getColor(context, R.color.c_D2D0DC)); etInput.setHintTextColor(color); } } /** * 初始化头部文字 * * @param context 上下文 * @param typedArray 资源对象 * @param text 头部文字 */ private void initHeaderText(Context context, TypedArray typedArray, CharSequence text) { // 头部字体风格 setViewTextStyle(tvHead, R.styleable.EditInputView_headerTextStyle, typedArray); // 头部字体颜色 setViewTextColor(context, tvHead, R.styleable.EditInputView_headerTextColor, typedArray); // 头部字体大小 setViewTextSize(tvHead, R.styleable.EditInputView_headerTextSize, typedArray); // 头部开始间距 int paddingStart = (int)typedArray.getDimension(R.styleable.EditInputView_headerPaddingStart, 0); // 头部结束间距 int paddingEnd = (int)typedArray.getDimension(R.styleable.EditInputView_headerPaddingEnd, 0); // 头部顶部间距 int paddingTop = (int)typedArray.getDimension(R.styleable.EditInputView_headerPaddingTop, 0); // 头部底部间距 int paddingBottom = (int)typedArray.getDimension(R.styleable.EditInputView_headerPaddingBottom, 0); tvHead.setText(text); tvHead.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom); } /** * 设置头部内容 * * @param text 被设置内容 */ public void setHeadText(CharSequence text) { if (tvHead != null) { tvHead.setText(text); } } /** * 获取内容 * * @return 内容 */ public CharSequence getText() { if (etInput == null) { return null; } else { return etInput.getText(); } } /** * 设置内容 * * @param text 被设置内容 */ public void setText(CharSequence text) { if (etInput != null) { etInput.setText(text); } } /** * 设置内容颜色 * * @param colorId 颜色资源Id */ public void setTextColor(@ColorRes int colorId) { if (etInput != null) { etInput.setTextColor(ContextCompat.getColor(getContext(), colorId)); } } /** * 设置最大输入限制 * * @param len 限制值 */ public void setMaxLength(int len) { etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(len)}); } public TextView getHeadTextView() { return tvHead; } public EditText getInputEditView() { return etInput; } }
以上就是Android 实现带头部文字输入框的自定义控件的详细内容,更多关于Android 文字输入框的自定义控件的资料请关注靠谱客其它相关文章!
最后
以上就是坦率面包最近收集整理的关于Android 实现带头部文字输入框的自定义控件的全部内容,更多相关Android内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复