使 TextInputEditText 默认聚焦
Make TextInputEditText focussed by default
我的 activity 中有一堆 TextInputEditText 字段。我希望最上面的一个在默认情况下成为焦点。但我无法让它工作。我的最低 SDK 版本是 21.
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:focusableInTouchMode="true"
tools:context=".MyActivity">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/myTitle"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="My Title"
app:counterEnabled="true"
app:counterMaxLength="20"
app:endIconMode="clear_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:boxBackgroundColor="@color/black"
android:textColorHint="@color/text_color"
app:boxStrokeWidthFocused="2dp"
app:boxStrokeColor="@drawable/outlined_box"
app:hintTextColor="@color/text_color"
android:focusedByDefault="true"
android:focusable="true"
app:endIconTint="@color/text_color"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:focusedByDefault="true"
android:textColor="@color/white"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
... more TextInputs
我的大纲框是一个选择器如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_focused="true"/>
<item android:color="@color/white" android:state_hovered="true"/>
<item android:color="@color/white" android:state_enabled="false"/>
<item android:color="@color/text_color"/> <!-- unfocused -->
</selector>
所有的例子都是网上看到的如何关闭默认对焦:)
尝试请求焦点:
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:focusedByDefault="true"
android:textColor="@color/white"
android:layout_height="wrap_content"
<requestFocus />
</com.google.android.material.textfield.TextInputEditText>
我的 activity 中有一堆 TextInputEditText 字段。我希望最上面的一个在默认情况下成为焦点。但我无法让它工作。我的最低 SDK 版本是 21.
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:focusableInTouchMode="true"
tools:context=".MyActivity">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/myTitle"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="My Title"
app:counterEnabled="true"
app:counterMaxLength="20"
app:endIconMode="clear_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:boxBackgroundColor="@color/black"
android:textColorHint="@color/text_color"
app:boxStrokeWidthFocused="2dp"
app:boxStrokeColor="@drawable/outlined_box"
app:hintTextColor="@color/text_color"
android:focusedByDefault="true"
android:focusable="true"
app:endIconTint="@color/text_color"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:focusedByDefault="true"
android:textColor="@color/white"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
... more TextInputs
我的大纲框是一个选择器如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_focused="true"/>
<item android:color="@color/white" android:state_hovered="true"/>
<item android:color="@color/white" android:state_enabled="false"/>
<item android:color="@color/text_color"/> <!-- unfocused -->
</selector>
所有的例子都是网上看到的如何关闭默认对焦:)
尝试请求焦点:
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:focusedByDefault="true"
android:textColor="@color/white"
android:layout_height="wrap_content"
<requestFocus />
</com.google.android.material.textfield.TextInputEditText>