ConstraintLayout:将EditText.requestFocus()添加到组中后无法正常工作

时间:2019-02-01 09:23:07

标签: android android-layout android-constraintlayout android-jetpack

EditText.requestFocus()工作不正常时,它在一个组被添加,

我删除了网上论坛,并且工作正常。

这不起作用:焦点仍然放在firstField上(焦点仅在第二次点击时焦点在secondField

someLayout.setOnClickListener {
    grSomething.visibility = Group.VISIBLE
    secondField.requestFocus()
}

<EditText
        android:id="@+id/firstField"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#0f0"
        app:layout_constraintWidth_percent=".5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/secondField"/>

<EditText
        android:id="@+id/secondField"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#00f"
        app:layout_constraintWidth_percent=".5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/firstField"/>

<android.support.constraint.Group
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/grSomething"
        android:visibility="gone"
        app:constraint_referenced_ids="secondField"/>

这是有效的:重点放在secondField

someLayout.setOnClickListener {
    secondField.visibility = View.VISIBLE
    secondField.requestFocus()
}

<EditText
        android:id="@+id/firstField"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#0f0"
        app:layout_constraintWidth_percent=".5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/secondField"/>

<EditText
        android:id="@+id/secondField"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#00f"
        android:visibility="gone"
        app:layout_constraintWidth_percent=".5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/firstField"/>

0 个答案:

没有答案