Android自定义视图:将属性委派给视图内部的控件

时间:2018-09-03 10:59:44

标签: android android-layout kotlin-android-extensions

我有一个自定义视图,其中包含三个控件(一个ImageView,一个TextInputLayout和一个TextInputEditText),并希望将我的自定义视图的xml标记属性委派给我的自定义视图中的相应视图。可以让我的自定义视图接受android:text =“ Some string” xml标签,然后使其在我的自定义视图内的相应TextInputEditText中显示字符串吗?

enter image description here

我的自定义视图(InputView.kt):

class InputView(context: Context, attrs: AttributeSet?, @AttrRes defStyleAttr: Int) : ConstraintLayout(context, attrs, defStyleAttr) {

    private lateinit var textInputLayout: TextInputLayout
    private lateinit var textView: TextInputEditText
    private lateinit var imageView: ImageView

//    var drawableRes = imageView.drawable ?: 0
    var text = textView.text
    var hint = textInputLayout.hint
    var error = textInputLayout.error
    var helperText = textInputLayout.helperText
    var ellipsize = textView.ellipsize ?: null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.custom_inputview, this, true)

        textInputLayout = view.custom_inputview_text_input_layout
        textView = view.custom_inputview_text_input_edit_text
        imageView = view.custom_inputview_image_view

        attrs.let {
            context.theme.obtainStyledAttributes(
                attrs,
                R.styleable.InputView,
                0, 0).apply {

                try {
//                    mShowText = getString(R.styleable.InputView_text)
                } finally {
                    recycle()
                }
            }
        }
    }

}

视图的布局文件(custom_inputview.xml):

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    tools:parentTag="com.lucasurbas.masterdetail.ui.persondetails.InputView"
    tools:ignore="ContentDescription">

    <ImageView
        android:id="@+id/custom_inputview_image_view"
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:tint="@color/bg_action_mode"
        tools:src="@drawable/ic_vd_hospital_24dp"/>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/custom_inputview_text_input_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        app:layout_constraintBottom_toBottomOf="@+id/custom_inputview_image_view"
        app:layout_constraintStart_toEndOf="@+id/custom_inputview_image_view"
        app:layout_constraintTop_toTopOf="@+id/custom_inputview_image_view">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/custom_inputview_text_input_edit_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:hint="Department"/>

    </com.google.android.material.textfield.TextInputLayout>
</merge>

2 个答案:

答案 0 :(得分:1)

如果您只想使用android样式属性,则可以执行以下操作。

在此示例中,如果您在创建InputView时在xml中定义了文本属性,则应该选择该文本属性。

init {
    attrs?.let {
        val typedArray = context.obtainStyledAttributes(attrs, STYLE_ATTRIBUTES)
        val text = typedArray.getText(0)
        typedArray.recycle()
    }
}

companion object {
    private val STYLE_ATTRIBUTES = intArrayOf(
        android.R.attr.text
    )
}

答案 1 :(得分:0)

这是我的工作解决方案。

+^(?:https?:\/\/)?(?:www\.)?123\.[a-zA-Z0-9.\S]+$
-^(?:https?:\/\/)?(?:www\.)?def\.[a-zA-Z0-9.\S]+$
-^(?:https?:\/\/)?(?:www\.)?ghi\.[a-zA-Z0-9.\S]+$

具有attr.xml中定义的属性:

class InputView(context: Context, attrs: AttributeSet?, @AttrRes defStyleAttr: Int) : ConstraintLayout(context, attrs, defStyleAttr) {

    private var textInputLayout: TextInputLayout
    private var textView: TextInputEditText
    private var imageView: ImageView

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.custom_inputview, this, true)

        textInputLayout = view.custom_inputview_text_input_layout
        textView = view.custom_inputview_text_input_edit_text
        imageView = view.custom_inputview_image_view

        attrs.let {
            context.theme.obtainStyledAttributes(
                it,
                R.styleable.InputView,
                defStyleAttr, 0).apply {

                try {
                    textView.setText(getText(R.styleable.InputView_android_text))
                    textInputLayout.hint = getText(R.styleable.InputView_android_hint)
                    Log.d("InputView", "attrs " + textView.text)
                } finally {
                    recycle()
                }
            }
        }
    }

}