Android 實(shí)現(xiàn)帶頭部文字輸入框的自定義控件
在app的輸入框中,需要應(yīng)用到很多帶有前綴說明的輸入框,運(yùn)用原有的輸入框和文本控件,一個(gè)帶頭部的輸入框就會增加三個(gè)控件在layout文件中。當(dāng)布局文件輸入框較少的情況下,這樣對后期維護(hù)影響不大,但在多個(gè)帶頭部的輸入框下,布局文件代碼量會很大,影響閱讀以及后期維護(hù)。而封裝過后的控件,在使用中僅僅需要幾行代碼可實(shí)現(xiàn)幾十行的效果。
簡介 帶頭部文字的輸入框 可在xml定義頭部文字樣式 可在xml定義輸入框樣式 可在xml定義提示文字樣式 可在xml定義頭部和輸入框的間距和邊距 效果圖<com.momin.common.widget.EditInputView android:layout_width='match_parent' android:layout_height='50dp' app:inputMarginStart='10dp' app:headerText='姓名' app:hint='請輸入聯(lián)系人姓名' app:inputType='text' app:maxLength='30'/>源碼在這
有幫助請點(diǎn)個(gè)贊
attrs.xml 屬性文檔
<!-- 公共屬性 --><!-- 前置文字內(nèi)容 --><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'> <!-- 文字內(nèi)容 --> <attr name='text' format='string'/> <!-- 文字大小 --> <attr name='textSize' format='dimension'/> <!-- 文字顏色 --> <attr name='textColor' format='reference|color'/> <!-- 最大輸入字符數(shù) --> <attr name='maxLength' format='integer'/> <!-- 輸入限制 --> <attr name='android:enabled'/> <!-- 輸入類型 --> <attr name='android:inputType'/> <!-- 輸入開始邊距 --> <attr name='inputMarginStart' format='dimension'/> <!-- 輸入結(jié)束邊距 --> <attr name='inputMarginEnd' format='dimension'/> <!-- 輸入頂部邊距 --> <attr name='inputMarginTop' format='dimension'/> <!-- 輸入底部邊距 --> <attr name='inputMarginBottom' format='dimension'/> <!-- 輸入開始間距 --> <attr name='inputPaddingStart' format='dimension'/> <!-- 輸入結(jié)束間距 --> <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'/> <!-- 前置文字內(nèi)容 --> <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 布局文件
<?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'> <!-- 頭部文字 --> <TextViewandroid: android:layout_width='wrap_content'android:layout_height='match_parent'android:gravity='start|center_vertical'/> <!-- 輸入框 --> <EditTextandroid: 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 控件類
package 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)) { // 頭部為空時(shí) tvHead.setVisibility(GONE);} else { // 頭部不為空時(shí) 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) {// 初始內(nèi)容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);// 設(shè)置間距setEditPadding(typedArray);// 設(shè)置邊距setEditMargin(typedArray);// 輸入類型限制setLimitInputType(typedArray);// 輸入長度限制setLimitInputLen(typedArray);// 輸入限制:可輸入性setInputBoolean(typedArray);// 輸入字體排列位置setInputGravity(typedArray);initEditHint(context, typedArray); } /** * 設(shè)置字體大小 * * @param view 被設(shè)置對象 * @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); } /** * 設(shè)置字體風(fēng)格 * * @param view 被設(shè)置對象 * @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)); } /** * 設(shè)置字體顏色 * * @param context 上下文 * @param view 被設(shè)置對象 * @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); } /** * 設(shè)置輸入框間距 * * @param typedArray 資源對象 */ private void setEditPadding(TypedArray typedArray) {// 開始間距int paddingStart = (int)typedArray.getDimension(R.styleable.EditInputView_inputPaddingStart, 0);// 結(jié)束間距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); } /** * 設(shè)置輸入框邊距 * * @param typedArray 資源對象 */ private void setEditMargin(TypedArray typedArray) {// 開始邊距int marginStart = (int)typedArray.getDimension(R.styleable.EditInputView_inputMarginStart, 0);// 結(jié)束邊距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); } /** * 設(shè)置輸入類型限制 * * @param typedArray 資源對象 */ private void setLimitInputType(TypedArray typedArray) {etInput.setInputType(typedArray.getInt(R.styleable.EditInputView_android_inputType, EditorInfo.TYPE_NULL)); } /** * 設(shè)置輸入長度限制 * * @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)) { // 提示文字不為空 // 提示內(nèi)容 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) {// 頭部字體風(fēng)格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);// 頭部結(jié)束間距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); } /** * 設(shè)置頭部內(nèi)容 * * @param text 被設(shè)置內(nèi)容 */ public void setHeadText(CharSequence text) {if (tvHead != null) { tvHead.setText(text);} } /** * 獲取內(nèi)容 * * @return 內(nèi)容 */ public CharSequence getText() {if (etInput == null) { return null;} else { return etInput.getText();} } /** * 設(shè)置內(nèi)容 * * @param text 被設(shè)置內(nèi)容 */ public void setText(CharSequence text) {if (etInput != null) { etInput.setText(text);} } /** * 設(shè)置內(nèi)容顏色 * * @param colorId 顏色資源Id */ public void setTextColor(@ColorRes int colorId) {if (etInput != null) { etInput.setTextColor(ContextCompat.getColor(getContext(), colorId));} } /** * 設(shè)置最大輸入限制 * * @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 實(shí)現(xiàn)帶頭部文字輸入框的自定義控件的詳細(xì)內(nèi)容,更多關(guān)于Android 文字輸入框的自定義控件的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式2. Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程3. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼4. php redis setnx分布式鎖簡單原理解析5. 《Java程序員修煉之道》作者Ben Evans:保守的設(shè)計(jì)思想是Java的最大優(yōu)勢6. CSS3中Transition屬性詳解以及示例分享7. bootstrap select2 動(dòng)態(tài)從后臺Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼8. 如何在PHP中讀寫文件9. java加載屬性配置properties文件的方法10. 什么是Python變量作用域
