目前,我有一個最簡單的一頁應用程式,只有一個資料輸入欄位。我看了很多例子,但我找不到我的問題的答案:如何在輸入欄位的底部添加提示文本?
<?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>

uj5u.com熱心網友回復:
<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>
uj5u.com熱心網友回復:
在您的 Edittext 下方添加一個 Textview 并將其約束
<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>
然后,如果您想根據用戶互動顯示/隱藏它,您可以在您的活動中執行類似的操作
editTextUserId.setOnFocusChangeListener((view, b) -> {
if(b){hint.setVisibility(View.VISIBLE);}
else {hint.setVisibility(View.INVISIBLE);}
});
enter code here
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/464816.html
