In Custom Compound view 'TextInputLayout '

2019-08-30 15:52发布

问题:

I've created a custom compound view. It contains an imageview, a TextInputLayout, EditText . The thing is that when i set attributes from xml, it sets all values like hint, icon etc but lines are not changed and expanded hint of TextInputLayout is not showing.

  1. When i set lines like this 'textInputLayout.getEditText().setLines(lines);' it works, but by using 'etInput.setLines(lines);' it doesn't work.

  2. Expanded hint is not working.

Widget:

public class InputLayoutWidget extends LinearLayout {

    private Context mContext;
    private TextInputLayout textInputLayout;
    private TextInputEditText etInput;
    private ImageView ivIcon;

    private String hint;
    private int inputType;
    private int lines;
    private boolean singleLine, isPasswordInput;
    private Drawable iconDrawable;

    public InputLayoutWidget(Context context) {
        super(context);
        initializeView(context, null, 0);
    }

    public InputLayoutWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializeView(context, attrs, 0);
    }

    public InputLayoutWidget(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initializeView(context, attrs, defStyleAttr);
    }

    private void initializeView(Context context, AttributeSet attrs, int defStyleAttr) {
        mContext = context;
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.InputLayoutWidget, defStyleAttr, 0);

        try {
            hint = array.getString(R.styleable.InputLayoutWidget_android_hint);
            lines = array.getInteger(R.styleable.InputLayoutWidget_android_lines, 1);
            singleLine = array.getBoolean(R.styleable.InputLayoutWidget_android_singleLine, false);
            isPasswordInput = array.getBoolean(R.styleable.InputLayoutWidget_isPassword, false);
            inputType = array.getInteger(R.styleable.InputLayoutWidget_android_inputType, InputType.TYPE_CLASS_TEXT);
            iconDrawable = array.getDrawable(R.styleable.InputLayoutWidget_iconDrawable);
        } finally {
            array.recycle();
        }

        setOrientation(HORIZONTAL);

        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.custom_input_field, this, true);

        AppLogger.d("usm_input_layout", "init");

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        AppLogger.d("usm_input_layout", "onFinishInflate");
        initComponents();
        setValues();
        addTextListener();
        //  invalidate();
    }


    private void initComponents() {

        textInputLayout = this.findViewById(R.id.textInputLayout);
        etInput = this.findViewById(R.id.et_input);
        ivIcon = this.findViewById(R.id.iv_icon);

        //etInput = textInputLayout.getEditText();

    }

    private void setValues() {
        etInput.setHint(hint);
        etInput.setSingleLine(singleLine);
        etInput.setLines(singleLine ? 1 : lines);
        etInput.setInputType(inputType);
        ivIcon.setImageDrawable(iconDrawable);

        //if(textInputLayout.getEditText()!=null)
        //textInputLayout.getEditText().setLines(singleLine ? 1 : lines);
    }


    public EditText getEditText() {
        return etInput;
    }

}

layout:

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

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/et_icon_margin_end"
        android:layout_marginTop="@dimen/et_icon_margin_top"
        android:contentDescription="@string/icon" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:errorEnabled="true">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/et_input"
            style="@style/TextInputEditStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
</merge>