电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android 實現帶頭部文字輸入框的自定義控件

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

在app的輸入框中,需要應用到很多帶有前綴說明的輸入框,運用原有的輸入框和文本控件,一個帶頭部的輸入框就會增加三個控件在layout文件中。當布局文件輸入框較少的情況下,這樣對后期維護影響不大,但在多個帶頭部的輸入框下,布局文件代碼量會很大,影響閱讀以及后期維護。而封裝過后的控件,在使用中僅僅需要幾行代碼可實現幾十行的效果。

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

Android 實現帶頭部文字輸入框的自定義控件

使用方法

<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 屬性文檔

<!-- 公共屬性 --><!-- 前置文字內容 --><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 布局文件

<?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)) { // 頭部為空時 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
相關文章:
主站蜘蛛池模板: 三轴曲线机-端子插拔力试验机|华杰仪器 | 上海三信|ph计|酸度计|电导率仪-艾科仪器 | 山东锐智科电检测仪器有限公司_超声波测厚仪,涂层测厚仪,里氏硬度计,电火花检漏仪,地下管线探测仪 | 云南成人高考_云南成考网| 深圳市八百通智能技术有限公司官方网站| 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | 细胞染色-流式双标-试剂盒免费代做-上海研谨生物科技有限公司 | 齿轮减速马达一体式_蜗轮蜗杆减速机配电机-德国BOSERL齿轮减速电动机生产厂家 | 全自动变压器变比组别测试仪-手持式直流电阻测试仪-上海来扬电气 | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | 行业分析:提及郑州火车站附近真有 特殊按摩 ?2025实地踩坑指南 新手如何避坑不踩雷 | 口臭的治疗方法,口臭怎么办,怎么除口臭,口臭的原因-口臭治疗网 | 篷房[仓储-婚庆-展览-活动]生产厂家-江苏正德装配式帐篷有限公司 | 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 东莞工作服_东莞工作服定制_工衣订做_东莞厂服 | 青岛空压机,青岛空压机维修/保养,青岛空压机销售/出租公司,青岛空压机厂家电话 | 三价铬_环保铬_环保电镀_东莞共盈新材料贸易有限公司 | RO反渗透设备_厂家_价格_河南郑州江宇环保科技有限公司 | 二手电脑回收_二手打印机回收_二手复印机回_硒鼓墨盒回收-广州益美二手电脑回收公司 | 涡街流量计_LUGB智能管道式高温防爆蒸汽温压补偿计量表-江苏凯铭仪表有限公司 | 自动记录数据电子台秤,记忆储存重量电子桌称,设定时间记录电子秤-昆山巨天 | 桐城新闻网—桐城市融媒体中心主办| 标准件-非标紧固件-不锈钢螺栓-非标不锈钢螺丝-非标螺母厂家-三角牙锁紧自攻-南京宝宇标准件有限公司 | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 | 小型铜米机-干式铜米机-杂线全自动铜米机-河南鑫世昌机械制造有限公司 | 水性绝缘漆_凡立水_绝缘漆树脂_环保绝缘漆-深圳维特利环保材料有限公司 | 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 | 全自动烧卖机厂家_饺子机_烧麦机价格_小笼汤包机_宁波江北阜欣食品机械有限公司 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 烽火安全网_加密软件、神盾软件官网| 净气型药品柜-试剂柜-无管道净气型通风柜-苏州毕恩思 | 二手回收公司_销毁处理公司_设备回收公司-找回收信息网 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 北京印刷厂_北京印刷_北京印刷公司_北京印刷厂家_北京东爵盛世印刷有限公司 | 派财经_聚焦数字经济内容服务平台| 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 成都APP开发-成都App定制-成都app开发公司-【未来久】 | 超声波气象站_防爆气象站_空气质量监测站_负氧离子检测仪-风途物联网 |