美文网首页
左侧文字输入框

左侧文字输入框

作者: 颤抖的闪电 | 来源:发表于2018-12-20 19:28 被阅读0次

效果如下


image.png
<com.qiyuan.gamesdk.core.ui.dialog.biz.View.TextInputBox
        android:layout_width="match_parent"
        android:layout_height="@dimen/dialog_edit_height"
        android:layout_marginBottom="@dimen/margin_16px"
        android:background="@drawable/qy_sdk_edt_bg"
        android:paddingLeft="@dimen/margin_24px"
        android:paddingRight="@dimen/margin_24px"
        app:leftText="原密码"
        app:leftTextColor="@color/qy_sdk_dialog_gray"
        app:rightHint="@string/register_password_hint"
        app:rightTextColor="@color/qy_sdk_user_center_bing_phone_tip"
        app:rightTextSize="@dimen/text_size_t4" />

主要框架代码如下:
TextInputBox.java

package com.qiyuan.gamesdk.core.ui.dialog.biz.View;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qiyuan.gamesdk.BuildConfig;
import com.qiyuan.gamesdk.R;
import com.qiyuan.gamesdk.core.CoreManager;

public class TextInputBox extends RelativeLayout {

    private TextView tv_left_text;
    private EditText edt_input;

    public TextInputBox(Context context) {
        this(context, null);
    }

    public TextInputBox(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextInputBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (BuildConfig.isApp) {
            LayoutInflater.from(CoreManager.getContext()).inflate(R.layout.text_input_box, this);
        } else {
            LayoutInflater.from(context).inflate(R.layout.text_input_box, this);
        }
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        edt_input = (EditText) findViewById(R.id.edt_input);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextInputBox, defStyleAttr, 0);
        int count = array.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.TextInputBox_leftText:
                    tv_left_text.setText(array.getString(attr));
                    break;
                case R.styleable.TextInputBox_leftTextColor:
                    tv_left_text.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.TextInputBox_leftTextSize:
                    tv_left_text.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;

                case R.styleable.TextInputBox_rightText:
                    edt_input.setText(array.getString(attr));
                    break;
                case R.styleable.TextInputBox_rightHint:
                    edt_input.setHint(array.getString(attr));
                    break;
                case R.styleable.TextInputBox_rightTextColor:
                    edt_input.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.TextInputBox_rightTextSize:
                    edt_input.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;
            }
        }
        array.recycle();
    }

}

布局文件text_input_box.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_left_text"
        android:layout_width="70dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical|left"
        android:text="左边:"
        tools:ignore="RtlHardcoded" />

    <EditText
        android:id="@+id/edt_input"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toEndOf="@id/tv_left_text"
        android:layout_toRightOf="@id/tv_left_text"
        android:background="@null"
        android:gravity="center_vertical|left"
        android:hint="右边输入框"
        android:textColorHint="#BFBFBF"
        android:textSize="12dp"
        tools:ignore="RtlHardcoded" />
</RelativeLayout>

在values文件夹下,还有属性文件text_input_box_attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--自定义属性-->
    <declare-styleable name="TextInputBox">
        <attr name="leftText" format="reference|string" />
        <attr name="leftTextColor" format="reference|color" />
        <attr name="leftTextSize" format="reference|dimension" />

        <attr name="rightText" format="reference|string" />
        <attr name="rightHint" format="reference|string" />
        <attr name="rightTextColor" format="reference|color" />
        <attr name="rightTextSize" format="reference|dimension" />
    </declare-styleable>
</resources>

在drawable文件夹下,还有个背景文件qy_sdk_edt_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke android:color="@color/edt_stroke"   //#FFBDBDBD
        android:width="@dimen/edt_stroke_width"/>  //0.5dp
    <corners
        android:radius="2dp"/>
    <solid android:color="@color/dk_transparent"/>  //#00000000

</shape>

至此主要的代码框架就完成了,代码功能可以继续完善,例如对于里面当个的控件添加单独的样式控制、点击事件等等。

相关文章

网友评论

      本文标题:左侧文字输入框

      本文链接:https://www.haomeiwen.com/subject/oojukqtx.html