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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章