0

我创建了一个自定义复合视图。它包含一个 imageview、一个 TextInputLayout、EditText。问题是,当我从 xml 设置属性时,它会设置所有值,如提示、图标等,但不会更改行,并且不会显示 TextInputLayout 的扩展提示。

  1. 当我设置这样的行时' textInputLayout.getEditText().setLines(lines); ' 它有效,但通过使用'etInput.setLines(lines);' 它不起作用。

  2. 扩展提示不起作用。

小部件:

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

}

布局:

<?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>
4

0 回答 0