您好,我创建了一个自定义视图,该视图扩展了下面的约束布局:
class CustomEditText : ConstraintLayout, TextWatcher {
override fun afterTextChanged(p0: Editable?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, android.R.attr.editTextStyle)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, android.R.attr.editTextStyle) {
init(context, attrs, android.R.attr.editTextStyle)
}
private fun init(context: Context, attrs: AttributeSet, defStyleAttr: Int) {
inflate(context, R.layout.my_edit_text, this)
val a = context.theme.obtainStyledAttributes(attrs, R.styleable.MyEditText, defStyleAttr, 0)
//if label text set, show it
//if desc text set show it
//set error text
//else pass the other attrubute sets to the editText as per normal
//editTextView.setAttributes????
}
}
此布局在xml中如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/labelText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/BoldTextAppearance"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Label Here" />
<TextView
android:id="@+id/descriptionText"
style="@style/MyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/labelText"
tools:text="description goes here" />
<ImageView
android:id="@+id/errorIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_verify_reg_error_bg"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/descriptionText" />
<TextView
android:id="@+id/errorText"
style="@style/MyTextViewError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/errorIcon"
app:layout_constraintTop_toBottomOf="@id/descriptionText"
tools:text="Error goes here" />
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/errorText">
<TextView
android:id="@+id/sideHintText"
style="@style/TextAppearance.grey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="123456" />
<ImageView
android:id="@+id/rightIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent" />
<EditText
android:id="@+id/editTextView"
style="@style/editTextNoBg"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintLeft_toRightOf="@id/sideHintText"
app:layout_constraintRight_toLeftOf="@+id/rightIcon" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
我创建了一些自定义属性,这些属性填充了称为descriptionText
,errorText
等的视图
<declare-styleable name="CustomEditText">
<attr name="leftHintText" format="string" />
<attr name="label" format="string" />
<attr name="description" format="string" />
<attr name="errorText" format="string" />
<attr name="rightActionSrc" format="reference" />
</declare-styleable>
使用此自定义视图时,我使用以下内容:
<com.main.main.ui.CustomEditText
android:id="@+id/titleOtherTextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_small"
android:inputType="text"
android:maxLength="5"
app:leftHintText="name"
app:label="firstName"
app:layout_constraintTop_toBottomOf="@+id/titleSpinner" />
我的问题是,如何将上面的默认android: attributes
(例如android:maxLength
,android:inputType
等)传递到位于自定义视图中的editTextView? / p>
我的理解是,只有父视图才能获得android: attributes
,即我的constraintLayout,但是我想知道是否可以将其中一些传递给editText,因为我不想复制诸如android:inputType
之类的东西和android:maxLength
(已在android中定义)。我只是想将这些传递给我正在膨胀的视图xml中的editText子视图。