国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android 實(shí)現(xiàn)帶頭部文字輸入框的自定義控件

瀏覽:24日期:2022-09-19 16:12:03
前言

在app的輸入框中,需要應(yīng)用到很多帶有前綴說(shuō)明的輸入框,運(yùn)用原有的輸入框和文本控件,一個(gè)帶頭部的輸入框就會(huì)增加三個(gè)控件在layout文件中。當(dāng)布局文件輸入框較少的情況下,這樣對(duì)后期維護(hù)影響不大,但在多個(gè)帶頭部的輸入框下,布局文件代碼量會(huì)很大,影響閱讀以及后期維護(hù)。而封裝過(guò)后的控件,在使用中僅僅需要幾行代碼可實(shí)現(xiàn)幾十行的效果。

簡(jiǎn)介 帶頭部文字的輸入框 可在xml定義頭部文字樣式 可在xml定義輸入框樣式 可在xml定義提示文字樣式 可在xml定義頭部和輸入框的間距和邊距 效果圖

Android 實(shí)現(xiàn)帶頭部文字輸入框的自定義控件

使用方法

<com.momin.common.widget.EditInputView android:layout_width='match_parent' android:layout_height='50dp' app:inputMarginStart='10dp' app:headerText='姓名' app:hint='請(qǐng)輸入聯(lián)系人姓名' app:inputType='text' app:maxLength='30'/>源碼在這

有幫助請(qǐng)點(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'/> <!-- 輸入類(lèi)型 --> <attr name='android:inputType'/> <!-- 輸入開(kāi)始邊距 --> <attr name='inputMarginStart' format='dimension'/> <!-- 輸入結(jié)束邊距 --> <attr name='inputMarginEnd' format='dimension'/> <!-- 輸入頂部邊距 --> <attr name='inputMarginTop' format='dimension'/> <!-- 輸入底部邊距 --> <attr name='inputMarginBottom' format='dimension'/> <!-- 輸入開(kāi)始間距 --> <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 控件類(lèi)

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) {// 初始化對(duì)象initView(context);// 獲取資源對(duì)象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);}// 回收資源對(duì)象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 資源對(duì)象 */ 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);// 輸入類(lèi)型限制setLimitInputType(typedArray);// 輸入長(zhǎng)度限制setLimitInputLen(typedArray);// 輸入限制:可輸入性setInputBoolean(typedArray);// 輸入字體排列位置setInputGravity(typedArray);initEditHint(context, typedArray); } /** * 設(shè)置字體大小 * * @param view 被設(shè)置對(duì)象 * @param attrId屬性Id * @param typedArray 資源對(duì)象 */ 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è)置對(duì)象 * @param attrId屬性Id * @param typedArray 資源對(duì)象 */ 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è)置對(duì)象 * @param attrId屬性Id * @param typedArray 資源對(duì)象 */ 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 資源對(duì)象 */ private void setEditPadding(TypedArray typedArray) {// 開(kāi)始間距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 資源對(duì)象 */ private void setEditMargin(TypedArray typedArray) {// 開(kāi)始邊距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è)置輸入類(lèi)型限制 * * @param typedArray 資源對(duì)象 */ private void setLimitInputType(TypedArray typedArray) {etInput.setInputType(typedArray.getInt(R.styleable.EditInputView_android_inputType, EditorInfo.TYPE_NULL)); } /** * 設(shè)置輸入長(zhǎng)度限制 * * @param typedArray 資源對(duì)象 */ private void setLimitInputLen(TypedArray typedArray) {int len = typedArray.getInteger(R.styleable.EditInputView_maxLength, 0);if (len > 0) { setMaxLength(len);} } /** * 輸入限制:可輸入性 * * @param typedArray 資源對(duì)象 */ private void setInputBoolean(TypedArray typedArray) {etInput.setEnabled(typedArray.getBoolean(R.styleable.EditInputView_android_enabled, true)); } /** * 輸入字體排列位置 * * @param typedArray 資源對(duì)象 */ private void setInputGravity(TypedArray typedArray) {etInput.setGravity(typedArray.getInt(R.styleable.EditInputView_android_gravity,Gravity.END|Gravity.CENTER_VERTICAL)); } /** * 初始化輸入框提示文字 * * @param context 上上下文 * @param typedArray 資源對(duì)象 */ 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 資源對(duì)象 * @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);// 頭部開(kāi)始間距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è)置頭部?jī)?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 文字輸入框的自定義控件的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 91日本在线精品高清观看 | 亚洲欧美片 | 一区二区精品视频 | 日韩美女免费视频 | 深夜福利成人 | 亚洲狠狠ady亚洲精品大秀 | 免费看特黄特黄欧美大片 | 黄色美女网站免费看 | 毛片网站大全 | 久久橹| avav在线看 | 成人精品一区二区三区中文字幕 | 欧美高清性色生活 | 久久伊人免费视频 | 久在线播放 | 免费黄网大全 | 中文乱码一二三四有限公司 | 男人干女人的视频 | 韩国精品一区二区三区四区五区 | 91手机看片国产福利精品 | 一级国产a级a毛片无卡 | 日本在线视频不卡 | 亚洲高清在线观看看片 | 真实的国产乱xxxx | 99热久久精品国产 | 久久久久毛片成人精品 | 国产亚洲视频在线播放大全 | 一级做a爰片久久毛片欧美 一级做a爰片久久毛片人呢 | 久久久综合久久 | 99精品视频在线这里只有 | 欧美色老头oldvideo | 欧美精品a毛片免费观看 | 欧美日韩精品一区二区在线线 | 精品亚洲一区二区三区 | 亚洲免费在线 | 久久久夜间小视频 | 日韩成人毛片高清视频免费看 | 国产成人综合亚洲欧美在 | 日韩不卡一区二区 | 国产黄色一级毛片 | 青青青青爽视频在线播放 |