在输入字段的下面添加提示文本

发布于 2025-01-24 14:37:39 字数 1224 浏览 2 评论 0原文

目前,我拥有一个具有一个数据输入字段的最简单的一页应用程序。我查看了很多示例,但是找不到问题的答案:如何在输入字段的底部添加提示文本?

<?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>

At the moment, I have the simplest one-page application with one data entry field. I looked at a lot of examples, but I could not find the answer to my question: how do I add hint text at the bottom 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回梦 2025-01-31 14:37:39
<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>
<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>
把时间冻结 2025-01-31 14:37:39

添加文本视图并将其限制在您的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>

下方
像这样的活动

editTextUserId.setOnFocusChangeListener((view, b) -> {
        if(b){hint.setVisibility(View.VISIBLE);}
        else {hint.setVisibility(View.INVISIBLE);}

    });

Add a Textview and Constrain it below your 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>

Then if you want to show/hide it base on user interaction you can do something
like this in your activity

editTextUserId.setOnFocusChangeListener((view, b) -> {
        if(b){hint.setVisibility(View.VISIBLE);}
        else {hint.setVisibility(View.INVISIBLE);}

    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文