MDTextInputLayout

1.自定义view类

public class MDTextInputLayout extends RelativeLayout implements TextWatcher {

    private EditText editText; //輸入
    private TextView textView; //底部展示

    public MDTextInputLayout(Context context) {
        super(context);
        init(context, null);
    }

    public MDTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MDTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTextInput, 0, 0);

        String bottomText = a.getString(R.styleable.CustomTextInput_bottomText);
        float bottomSize = a.getDimension(R.styleable.CustomTextInput_bottomSize, 12);
        int bottomColor = a.getColor(R.styleable.CustomTextInput_bottomColor, Color.BLUE);

        Logger.i(bottomSize + "  size");
        editText = new EditText(context);
        editText.setHint(bottomText);
        editText.setPadding(0, 10, 10, 10);
        editText.setTextSize(14);
        editText.setBackgroundColor(Color.WHITE);
        textView = new TextView(context);
        textView.setTextSize(bottomSize);
        textView.setText(bottomText);
        textView.setTextColor(bottomColor);

        editText.addTextChangedListener(this);
        addView(textView);
        addView(editText);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /**
         * 获得此ViewGroup上级容器为其推荐的宽和高,以及计算模式
         */
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

        // 计算出所有的childView的宽和高
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        /**
         * 记录如果是wrap_content是设置的宽和高
         */
        int height = 0;

        MarginLayoutParams cParams;

        /**
         * 根据childView计算的出的宽和高,以及设置的margin计算容器的宽和高,主要用于容器是warp_content时
         */
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            int cHeight = childView.getMeasuredHeight();
            cParams = (MarginLayoutParams) childView.getLayoutParams();
            height += cHeight + cParams.topMargin + cParams.bottomMargin;
        }

        /**
         * 如果是wrap_content设置为我们计算的值
         * 否则:直接设置为父容器计算的值
         */
        setMeasuredDimension(getMeasuredWidth(), (heightMode == MeasureSpec.EXACTLY) ? sizeHeight : height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int height = editText.getMeasuredHeight();
        final int paddingLeft = editText.getPaddingLeft();
        final int paddingTop = editText.getPaddingTop();
        String src = editText.getText().toString();

        editText.layout(0,
                (getHeight() - editText.getMeasuredHeight()) / 2,
                getMeasuredWidth(),
                (getHeight() + editText.getMeasuredHeight()) / 2);

        if (src.length() == 0) {
            editText.layout(0,
                    (getHeight() - editText.getMeasuredHeight()) / 2,
                    getMeasuredWidth(),
                    (getHeight() + editText.getMeasuredHeight()) / 2);
            textView.setVisibility(GONE);
            textView.layout(paddingLeft, paddingTop, getMeasuredWidth(), textView.getMeasuredHeight() + paddingTop);

        } else {
            editText.layout(0, 0, getMeasuredWidth(), editText.getMeasuredHeight());
            textView.setVisibility(VISIBLE);
            textView.layout(paddingLeft, height, getMeasuredWidth(), textView.getMeasuredHeight() + height);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        postInvalidate();
    }


    public String getInputText() {
        return editText.getText().toString();
    }

2.定义属性

 <declare-styleable name="CustomTextInput">
        <attr name="bottomColor" format="color" />
        <attr name="bottomText" format="string" />
        <attr name="bottomSize" format="dimension" />
</declare-styleable>
3.使用
 <LinearLayout style="@style/register_linear_trainee_layout">

        <TextView
            style="@style/register_label"
            android:text="密码" />

        <com.ycx.driver.utils.widget.view.MDTextInputLayout
            android:id="@+id/milPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/color_white"
            coustom:bottomColor="@color/color_register_error"
            coustom:bottomSize="12px"
            coustom:bottomText="请输入在东方时尚注册时所填密码">

        </com.ycx.driver.utils.widget.view.MDTextInputLayout>
    </LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值