Android 實(shí)現(xiàn)帶頭部文字輸入框的自定義控件
前言
在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定義頭部和輸入框的間距和邊距
效果圖

使用方法
<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"/>
<!-- 輸入類型 -->
<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">
<!-- 頭部文字 -->
<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 控件類
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);
// 輸入類型限制
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è)置輸入類型限制
*
* @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)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android開(kāi)發(fā)事件處理的代碼如何寫(xiě)手摸手教程
這篇文章主要為大家介紹了Android開(kāi)發(fā)事件處理的代碼如何寫(xiě)手摸手教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
快速解決進(jìn)入fragment時(shí)不能彈出軟件盤(pán)的問(wèn)題
下面小編就為大家?guī)?lái)一篇快速解決進(jìn)入fragment時(shí)不能彈出軟件盤(pán)的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Android開(kāi)發(fā)筆記之:深入理解多線程AsyncTask
本篇文章是對(duì)Android中多線程AsyncTask進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android自定義控件開(kāi)發(fā)實(shí)戰(zhàn)之實(shí)現(xiàn)ListView下拉刷新實(shí)例代碼
這篇文章主要介紹了Android自定義控件開(kāi)發(fā)實(shí)戰(zhàn)之實(shí)現(xiàn)ListView下拉刷新實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04
深入Android中BroadcastReceiver的兩種注冊(cè)方式(靜態(tài)和動(dòng)態(tài))詳解
這篇文章主要介紹了深入Android中BroadcastReceiver的兩種注冊(cè)方式(靜態(tài)和動(dòng)態(tài))詳解,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12
android實(shí)現(xiàn)藍(lán)牙app代碼
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)藍(lán)牙app的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Cocos2d-x入門教程(詳細(xì)的實(shí)例和講解)
這篇文章主要介紹了Cocos2d-x入門教程,包括詳細(xì)的實(shí)例、講解以及實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下2014-04-04
Android實(shí)現(xiàn)清除應(yīng)用緩存功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)清除應(yīng)用緩存功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

