【问题标题】:Hint and User Input overlaying in a TextViewTextView 中的提示和用户输入叠加
【发布时间】:2021-09-11 06:24:05
【问题描述】:

我以前从未见过这种情况,也不知道是什么原因造成的,我的第一选择是在片段中用一段 java 代码隐藏提示,但我失败了,现在我只想找到一个 XML 解决方案。

这发生在每个领域,我找不到这里的布局有什么问题,有什么见解吗?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp"
    android:orientation="vertical">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/faq_contato_nome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_nome_contato"
            android:inputType="textPersonName"
            android:lines="1"
            android:padding="10dp"
            android:textSize="15sp" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/faq_contato_telefone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_telefone_contato"
            android:inputType="phone"
            android:lines="1"
            android:padding="10dip"
            android:textSize="15sp" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/faq_contato_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_email_contato"
            android:inputType="textEmailAddress"
            android:lines="1"
            android:padding="10dip"
            android:textSize="15sp" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/faq_contato_nome_empresa"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_empresa_contato"
            android:inputType="text"
            android:lines="1"
            android:padding="10dip"
            android:textSize="15sp" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/faq_contato_telefone_empresa"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_tel_empresa_contato"
            android:inputType="phone"
            android:lines="1"
            android:padding="10dip"
            android:textSize="15sp" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/faq_contato_matricula"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Digite sua matricula (Opcional)"
            android:inputType="number"
            android:lines="1"
            android:padding="10dip"
            android:textSize="15sp" />
    </com.google.android.material.textfield.TextInputLayout>

    <sinapse.com.br.estilo.component.CustonSpinner
        android:id="@+id/faq_contato_spinner_assunto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:hintText="@string/title_spinner" />

    <EditText
        android:id="@+id/faq_contato_campo_mensagem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:layout_marginEnd="2dp"
        android:layout_marginStart="2dp"
        android:gravity="center_horizontal|bottom"
        android:hint="@string/hint_areamensagem"
        android:inputType="textMultiLine" />

    <TextView
        android:id="@+id/faq_contato_numero_caracteres"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="@string/caracteres" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/faq_contato_btn_enviar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:layout_gravity="right"
        android:text="@string/enviar" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/faq_contato_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="13dp" />
</LinearLayout>
</ScrollView>

【问题讨论】:

  • 如果您使用 TextInputLayout,请始终仅在 TextInputLayout 中提供提示,而不是在编辑文本中提供提示。

标签: java android xml android-studio kotlin


【解决方案1】:

问题是您为EditText 设置了一个填充,这就是为什么EditText 组件没有被给予足够的空间来容纳用户的输入,而不必覆盖提示。只需去掉 EditText 组件中的填充,它就会运行得很好。 :)

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/faq_contato_telefone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_telefone_contato"
            android:inputType="phone"
            android:lines="1"
            android:textSize="15sp" />
    </com.google.android.material.textfield.TextInputLayout>

如果您不希望提示浮动。 (如果您希望用户点击EditText 框后提示完全消失)

您可以在您的TextInputLayout 中添加app:hintEnabled="false"。 在这种情况下,我认为您可以安全地重新添加填充,因为提示将在用户点击时消失。

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:hintEnabled="false">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/faq_contato_telefone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_telefone_contato"
            android:inputType="phone"
            android:lines="1"
            android:padding="10dp"
            android:textSize="15sp" />
    </com.google.android.material.textfield.TextInputLayout>

【讨论】:

    【解决方案2】:

    试试下面的代码可能对你有帮助。

    //Remove padding from edittext
    //Try to set hint on TextInputLayout instead of EditText
    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_nome_contato">
    
     <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/faq_contato_nome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:lines="1"
            android:textSize="15sp" />
        
    </com.google.android.material.textfield.TextInputLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多