自定义复合视图内TextInputLayout出现问题

时间:2018-08-10 09:54:29

标签: android android-layout android-custom-view android-textinputlayout android-compound-view

TextInputLayout在“自定义复合视图”中的行为异常。它不会显示扩展的提示,并且在设置行数$时在自定义视图中不起作用,但是etInput.setLines(lines)起作用。我在做什么错也许我需要使用onDraw或onMeasure做一些事情,但是如何? 以下是我的代码:

Java:

textInputLayout.getEditText().setLines(lines);

xml:

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;
    }

}

0 个答案:

没有答案