在输入框下方添加提示文字
Add hint text at the below of the input field
目前,我有一个最简单的单页应用程序,只有一个数据输入字段。我看了很多例子,但找不到我的问题的答案:如何在输入字段的底部添加提示文本?
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextUserId"
android:layout_width="347dp"
android:layout_height="60dp"
android:layout_marginBottom="20dp"
android:ems="10"
android:hint="User"
android:inputType="textPersonName"
android:text="@string/user_id"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/imageButton"
app:layout_constraintEnd_toEndOf="@+id/editTextDeviveId"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/editTextSessionId"
app:layout_constraintTop_toBottomOf="@+id/editTextDeviveId"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_customer_no"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
app:helperTextEnabled="true"
app:helperText="User">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextUserId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Customer No"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
添加一个 Textview 并将其限制在您的 Edittext 下方
<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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextUserId"
android:layout_width="347dp"
android:layout_height="60dp"
android:layout_marginBottom="20dp"
android:ems="10"
android:hint="User"
android:inputType="textPersonName"
android:text="@string/user_id"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/imageButton"
app:layout_constraintEnd_toEndOf="@+id/editTextDeviveId"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/editTextSessionId"
app:layout_constraintTop_toBottomOf="@+id/editTextDeviveId"
/>
<TextView
android:id="@+id/hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="user"
android:textColor="@android:color/holo_red_dark"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="@+id/editTextUserId"
app:layout_constraintTop_toBottomOf="@+id/editTextUserId" />
</androidx.constraintlayout.widget.ConstraintLayout>
然后如果你想 show/hide 它基于用户交互你可以做一些事情
在你的 activity
中这样
editTextUserId.setOnFocusChangeListener((view, b) -> {
if(b){hint.setVisibility(View.VISIBLE);}
else {hint.setVisibility(View.INVISIBLE);}
});
目前,我有一个最简单的单页应用程序,只有一个数据输入字段。我看了很多例子,但找不到我的问题的答案:如何在输入字段的底部添加提示文本?
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextUserId"
android:layout_width="347dp"
android:layout_height="60dp"
android:layout_marginBottom="20dp"
android:ems="10"
android:hint="User"
android:inputType="textPersonName"
android:text="@string/user_id"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/imageButton"
app:layout_constraintEnd_toEndOf="@+id/editTextDeviveId"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/editTextSessionId"
app:layout_constraintTop_toBottomOf="@+id/editTextDeviveId"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_customer_no"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
app:helperTextEnabled="true"
app:helperText="User">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextUserId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Customer No"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
添加一个 Textview 并将其限制在您的 Edittext 下方
<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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextUserId"
android:layout_width="347dp"
android:layout_height="60dp"
android:layout_marginBottom="20dp"
android:ems="10"
android:hint="User"
android:inputType="textPersonName"
android:text="@string/user_id"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/imageButton"
app:layout_constraintEnd_toEndOf="@+id/editTextDeviveId"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/editTextSessionId"
app:layout_constraintTop_toBottomOf="@+id/editTextDeviveId"
/>
<TextView
android:id="@+id/hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="user"
android:textColor="@android:color/holo_red_dark"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="@+id/editTextUserId"
app:layout_constraintTop_toBottomOf="@+id/editTextUserId" />
</androidx.constraintlayout.widget.ConstraintLayout>
然后如果你想 show/hide 它基于用户交互你可以做一些事情 在你的 activity
中这样editTextUserId.setOnFocusChangeListener((view, b) -> {
if(b){hint.setVisibility(View.VISIBLE);}
else {hint.setVisibility(View.INVISIBLE);}
});