我创建了一个具有以下布局的自定义视图:
<androidx.constraintlayout.widget.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:id="@+id/hintTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/hintTextInputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:text="hint"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<ImageView
android:id="@+id/hintTextInputInputIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/hint_text_input_input_icon_margin_end"
android:scaleType="centerInside"
tools:src="@drawable/ic_eyeopen"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/hintTextInputEditText"
app:layout_constraintBottom_toBottomOf="@+id/hintTextInputEditText"/>
<TextView
android:id="@+id/hintTextInputPlaceholder"
android:layout_width="0dp"
android:layout_height="0dp"
android:lines="1"
android:gravity="top"
android:layout_marginEnd="@dimen/hint_text_input_input_placeholder_margin_end"
tools:text="placeholder"
app:layout_constraintTop_toTopOf="@+id/hintTextInputEditText"
app:layout_constraintBottom_toBottomOf="@+id/hintTextInputEditText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/hintTextInputInputIcon"/>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/hintTextInputEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toStartOf="@+id/hintTextInputInputIcon"
app:layout_constraintTop_toBottomOf="@+id/hintTextInputText"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="@dimen/hint_text_input_input_margin_top"
android:layout_marginEnd="@dimen/hint_text_input_input_margin_end"
android:lines="1"
android:background="@null"
android:textCursorDrawable="@null"
tools:text="edit"/
</androidx.constraintlayout.widget.ConstraintLayout>
我也在attrs.xml文件中定义了视图:
<declare-styleable name="HintTextInputView">
<attr name="hint_text_size" format="dimension"/>
<attr name="input_text_size" format="dimension"/>
<attr name="hint_text_color" format="color"/>
<attr name="input_text_color" format="color"/>
<attr name="input_hint_text_color" format="color"/>
<attr name="android:text"/>
<attr name="android:nextFocusForward"/>
<attr name="hint_text" format="string"/>
<attr name="input_hint_text" format="string"/>
<attr name="hint_text_style" format="enum">
<enum name="normal" value="0"/>
<enum name="bold" value="1"/>
<enum name="italic" value="2"/>
</attr>
<attr name="input_text_style" format="enum">
<enum name="normal" value="0"/>
<enum name="bold" value="1"/>
<enum name="italic" value="2"/>
</attr>
<attr name="enable_input_icon" format="boolean"/>
<attr name="input_icon_enabled_image" format="integer"/>
<attr name="input_icon_disabled_image" format="integer"/>
<attr name="input_icon_tint_color" format="color"/>
<attr name="enable_borders" format="boolean"/>
<attr name="border_color" format="color"/>
<attr name="focused_border_color" format="color"/>
<attr name="border_radius" format="dimension"/>
<attr name="android:inputType"/>
</declare-styleable>
在任何布局中定义属性时,我也会通过编程方式将属性设置为EdditText:
private fun TextView.setFocusForward(attributes: TypedArray) {
attributes.getResourceId(R.styleable.HintTextInputView_android_nextFocusForward, 0).let {
if (it != 0) {
this.nextFocusForwardId = it
}
}
}
具有视图的XML如下所示:
<br.sis.smb.base.ui.HintTextInputView
android:id="@+id/name"
style="@style/DefaultEditTextBorder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/register_company_margin_start_end"
android:layout_marginTop="@dimen/register_company_edittext_top_margin_top"
android:layout_marginEnd="@dimen/register_company_margin_space_between"
android:text="@={viewModel.name}"
android:nextFocusForward="@id/firstNickname"
app:hint_text="@string/register_edittext_name_hint"
app:input_hint_text="@string/register_edittext_name"
app:layout_constraintEnd_toStartOf="@+id/firstNickname"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4"
/>
<br.sis.smb.base.ui.HintTextInputView
android:id="@+id/firstNickname"
style="@style/DefaultEditTextBorder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/register_company_margin_space_between"
android:layout_marginEnd="@dimen/register_company_margin_start_end"
android:imeOptions="actionNext"
android:nextFocusForward="@+id/secondNickname"
android:text="@={viewModel.nickname}"
app:hint_text="@string/register_edittext_nickname_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/name"
app:layout_constraintTop_toTopOf="@+id/name"
app:layout_constraintVertical_bias="0.4"
/>
问题是android:nextFocusForward属性根本不起作用。即使我定义了上面的代码,但是当我在设备键盘上触摸下一步时,它并不会导致正确的自定义视图,而是导致以下自定义视图。
答案 0 :(得分:1)
请确保您没有在仿真器上运行,并且在某些仿真器的nextfocusforward
上不起作用,在这种情况下,您必须使用nextFocusDown
。
还应该存在singleLine = true,而imeOptions = actionNext
答案 1 :(得分:0)
确保没有设置inputType:'none'
或'0x00000000'