Привет, я создал настраиваемый вид, расширяющий макет ограничения ниже:
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, расположенный внутри моего пользовательского представления?
Я понимаю, что только родительский вид получает android: attributes, то есть мой constraintLayout, но мне было интересно, могу ли я передать некоторые из них в editText, поскольку я не хочу дублировать такие вещи, как android:inputType и android:maxLength, когда он уже определен в android. Я просто хочу передать их дочернему представлению editText, расположенному в представлении xml, которое я раздуваю.
Через раздувание взгляда. Вы создаете представление вручную, только если вы программно создаете свои макеты в коде, а не в xml.
Нет, это не пусто, я опубликовал полный макет этого выше. пожалуйста, проверьте и прочтите мой код выше
EditText не создается мной. он автоматически создается в Android, как только я надуваю настраиваемый вид.