Android- Custom controls – Header text input box

preface

In the input box of app, it needs to be applied to many input boxes with prefix description. Using the original input box and text control, an input box in the head will add three controls in the layout file. When the layout file input box is less, this has little impact on the later maintenance, but in the input box of multiple heads, the layout file code amount will be large, affecting reading and later maintenance. And the control after encapsulation, in the use of only a few lines of code can achieve dozens of lines of effect.

Introduction to the

  1. Header text input box
  2. Header text styles can be defined in XML
  3. Input box styles can be defined in XML
  4. Prompt text styles can be defined in XML
  5. You can define spacing and margins for headers and input boxes in XML

rendering

Method of use

<com.momin.common.widget.EditInputView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    app:inputMarginStart="10dp"
    app:headerText="Name"
    app:hint="Please enter contact name"
    app:inputType="text"
    app:maxLength="30"/>
Copy the code

The source code in this

Please give it a thumbs up if it helps

Attrs.xml attribute document

<! -- Public properties -->
<! -- Prefacing text -->
<attr name="headerText" format="string"/>
<! -- Font size -->
<attr name="headerTextSize" format="dimension"/>
<! -- Font size -->
<attr name="headerTextStyle">
    <flag name="normal" value="0" />
    <flag name="bold" value="1" />
    <flag name="italic" value="2" />
</attr>
<! -- Text color -->
<attr name="headerTextColor" format="reference|color"/>
<! -- Left margin of leading text -->
<attr name="headerPaddingStart" format="dimension"/>
<! -- Right margin of front text -->
<attr name="headerPaddingEnd" format="dimension"/>
<! -- Top spacing of prefacing text -->
<attr name="headerPaddingTop" format="dimension"/>
<! -- Bottom spacing of front text -->
<attr name="headerPaddingBottom" format="dimension"/>
<! -- Public properties -->

<! -- Input box with prefacing text -->
<declare-styleable name="EditInputView">
    <! -- Text content -->
    <attr name="text" format="string"/>
    <! -- Text size -->
    <attr name="textSize" format="dimension"/>
    <! -- Text color -->
    <attr name="textColor" format="reference|color"/>
    <! -- Maximum number of characters -->
    <attr name="maxLength" format="integer"/>
    <! -- Input limits -->
    <attr name="android:enabled"/>
    <! -- Input type -->
    <attr name="android:inputType"/>
    <! -- Input start margin -->
    <attr name="inputMarginStart" format="dimension"/>
    <! -- Input end margin -->
    <attr name="inputMarginEnd" format="dimension"/>
    <! Enter the top margin -->
    <attr name="inputMarginTop" format="dimension"/>
    <! Enter the bottom margin -->
    <attr name="inputMarginBottom" format="dimension"/>
    <! -- Input start spacing -->
    <attr name="inputPaddingStart" format="dimension"/>
    <! -- Input end spacing -->
    <attr name="inputPaddingEnd" format="dimension"/>
    <! -- Enter the top spacing -->
    <attr name="inputPaddingTop" format="dimension"/>
    <! -- Input bottom spacing -->
    <attr name="inputPaddingBottom" format="dimension"/>
    <! -- Input bottom spacing -->
    <attr name="android:gravity"/>
    <! -- Prompt text -->
    <attr name="hint" format="string"/>
    <! -- Prompt text color -->
    <attr name="hintColor" format="reference|color"/>
    <! -- Prefacing text -->
    <attr name="headerText"/>
    <! -- Font size -->
    <attr name="headerTextSize"/>
    <! -- Font size -->
    <attr name="headerTextStyle"/>
    <! -- Text color -->
    <attr name="headerTextColor"/>
    <! -- Left margin of leading text -->
    <attr name="headerPaddingStart"/>
    <! -- Right margin of front text -->
    <attr name="headerPaddingEnd"/>
    <! -- Top spacing of prefacing text -->
    <attr name="headerPaddingTop"/>
    <! -- Bottom spacing of front text -->
    <attr name="headerPaddingBottom"/>
</declare-styleable>
Copy the code

Common_edit_input_view.xml layout file


      
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <! -- Header text -->
    <TextView
        android:id="@+id/tv_edit_head"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="start|center_vertical"/>
    <! -- Input box -->
    <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>
Copy the code

EditInputView. Java control class

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 > the Description: the head box * < / p > < p > Copyright: * < / p > < p > Company: < / p > * *@author Momin
 * @version 1.0
 * @date2021/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);
    }

    /** * initializes **@paramContext *@paramAttrs resources * /
    private void init(Context context, AttributeSet attrs) {
        // Initialize the object
        initView(context);
        // Get the resource object
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EditInputView);
        // Initialize the input box
        initEdit(context, typedArray);

        // Initialize the header text
        CharSequence headText = typedArray.getText(R.styleable.EditInputView_headerText);
        if (TextUtils.isEmpty(headText)) {
            // The head is empty
            tvHead.setVisibility(GONE);
        } else {
            // The head is not empty
            tvHead.setVisibility(VISIBLE);
            initHeaderText(context, typedArray, headText);
        }

        // Reclaim the resource object
        typedArray.recycle();
    }

    /** * Initializes the view **@paramContext 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);
    }

    /** * Initializes the input box **@paramContext *@paramTypedArray Resource object */
    private void initEdit(Context context, TypedArray typedArray) {
        // Initial content
        CharSequence editText = typedArray.getText(R.styleable.EditInputView_text);
        if(! TextUtils.isEmpty(editText)) { etInput.setText(editText); }// Font size
        setViewTextSize(etInput, R.styleable.EditInputView_textSize, typedArray);
        // Font color
        setViewTextColor(context, etInput, R.styleable.EditInputView_textColor, typedArray);
        // Set the spacing
        setEditPadding(typedArray);
        // Set the margin
        setEditMargin(typedArray);
        // Input type restriction
        setLimitInputType(typedArray);
        // Input length limit
        setLimitInputLen(typedArray);
        // Input restriction: inputtable
        setInputBoolean(typedArray);
        // Enter the font arrangement
        setInputGravity(typedArray);

        initEditHint(context, typedArray);
    }

    /** * Sets the font size **@paramView is set to object *@paramAttrId Attribute Id *@paramTypedArray Resource object */
    private void setViewTextSize(TextView view, int attrId, TypedArray typedArray) {
        float size = typedArray.getDimension(attrId, 14 * view.getPaint().density);
        view.getPaint().setTextSize(size);
    }

    /** * Sets the font style **@paramView is set to object *@paramAttrId Attribute Id *@paramTypedArray Resource object */
    private void setViewTextStyle(TextView view, int attrId, TypedArray typedArray) {
        int style = typedArray.getInt(attrId, Typeface.NORMAL);
        view.setTypeface(Typeface.defaultFromStyle(style));
    }

    /** * Sets the font color **@paramContext *@paramView is set to object *@paramAttrId Attribute Id *@paramTypedArray Resource object */
    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);
    }

    /** * Sets input box spacing **@paramTypedArray Resource object */
    private void setEditPadding(TypedArray typedArray) {
        // Start spacing
        int paddingStart = (int)typedArray.getDimension(R.styleable.EditInputView_inputPaddingStart, 0);
        // End the spacing
        int paddingEnd = (int)typedArray.getDimension(R.styleable.EditInputView_inputPaddingEnd, 0);
        // Top spacing
        int paddingTop = (int)typedArray.getDimension(R.styleable.EditInputView_inputPaddingTop, 0);
        // Bottom spacing
        int paddingBottom = (int)typedArray.getDimension(R.styleable.EditInputView_inputPaddingBottom, 0);
        etInput.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom);
    }

    /** * Sets the input box margin **@paramTypedArray Resource object */
    private void setEditMargin(TypedArray typedArray) {
        // Start margin
        int marginStart = (int)typedArray.getDimension(R.styleable.EditInputView_inputMarginStart, 0);
        // End margin
        int marginEnd = (int)typedArray.getDimension(R.styleable.EditInputView_inputMarginEnd, 0);
        // Top margin
        int marginTop = (int)typedArray.getDimension(R.styleable.EditInputView_inputMarginTop, 0);
        // Bottom margin
        int marginBottom = (int)typedArray.getDimension(R.styleable.EditInputView_inputMarginBottom, 0);
        LayoutParams layoutParams = (LayoutParams)etInput.getLayoutParams();
        layoutParams.setMargins(marginStart, marginTop, marginEnd, marginBottom);
        etInput.setLayoutParams(layoutParams);
    }

    /** * Sets the input type limit **@paramTypedArray Resource object */
    private void setLimitInputType(TypedArray typedArray) {
        etInput.setInputType(typedArray.getInt(R.styleable.EditInputView_android_inputType, EditorInfo.TYPE_NULL));
    }

    /** * Sets the input length limit **@paramTypedArray Resource object */
    private void setLimitInputLen(TypedArray typedArray) {
        int len = typedArray.getInteger(R.styleable.EditInputView_maxLength, 0);
        if (len > 0) { setMaxLength(len); }}/** * Input restrictions: inputable **@paramTypedArray Resource object */
    private void setInputBoolean(TypedArray typedArray) {
        etInput.setEnabled(typedArray.getBoolean(R.styleable.EditInputView_android_enabled, true));
    }

    /** * Enter the font position **@paramTypedArray Resource object */
    private void setInputGravity(TypedArray typedArray) {
        etInput.setGravity(typedArray.getInt(R.styleable.EditInputView_android_gravity,
                Gravity.END|Gravity.CENTER_VERTICAL));
    }

    /** * Initializes the text ** in the input box@paramContext on context *@paramTypedArray Resource object */
    private void initEditHint(Context context, TypedArray typedArray) {
        CharSequence hintText = typedArray.getText(R.styleable.EditInputView_hint);
        if(! TextUtils.isEmpty(hintText)) {// The prompt text is not empty
            // Prompt content
            etInput.setHint(hintText);
            // Prompt text color
            intcolor = typedArray.getColor(R.styleable.EditInputView_hintColor, ContextCompat.getColor(context, R.color.c_D2D0DC)); etInput.setHintTextColor(color); }}/** * Initializes header text **@paramContext *@paramTypedArray Resource object *@paramText Header text */
    private void initHeaderText(Context context, TypedArray typedArray, CharSequence text) {
        // Header font style
        setViewTextStyle(tvHead, R.styleable.EditInputView_headerTextStyle, typedArray);
        // Header font color
        setViewTextColor(context, tvHead, R.styleable.EditInputView_headerTextColor, typedArray);
        // Header font size
        setViewTextSize(tvHead, R.styleable.EditInputView_headerTextSize, typedArray);
        // Start spacing between heads
        int paddingStart = (int)typedArray.getDimension(R.styleable.EditInputView_headerPaddingStart, 0);
        // End spacing of the head
        int paddingEnd = (int)typedArray.getDimension(R.styleable.EditInputView_headerPaddingEnd, 0);
        // Top spacing of the head
        int paddingTop = (int)typedArray.getDimension(R.styleable.EditInputView_headerPaddingTop, 0);
        // Spacing at the bottom of the head
        int paddingBottom = (int)typedArray.getDimension(R.styleable.EditInputView_headerPaddingBottom, 0);

        tvHead.setText(text);
        tvHead.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom);
    }

    /** * sets the header content **@paramText Set content */
    public void setHeadText(CharSequence text) {
        if(tvHead ! =null) { tvHead.setText(text); }}/** * get content **@returnContent * /
    public CharSequence getText(a) {
        if (etInput == null) {
            return null;
        } else {
            returnetInput.getText(); }}/** * Set the content **@paramText Set content */
    public void setText(CharSequence text) {
        if(etInput ! =null) { etInput.setText(text); }}/** * Sets the content color **@paramColorId Indicates the color resource Id */
    public void setTextColor(@ColorRes int colorId) {
        if(etInput ! =null) { etInput.setTextColor(ContextCompat.getColor(getContext(), colorId)); }}/** * Sets the maximum input limit **@paramLen limits the value */
    public void setMaxLength(int len) {
        etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(len)});
    }

    public TextView getHeadTextView(a) {
        return tvHead;
    }

    public EditText getInputEditView(a) {
        returnetInput; }}Copy the code