【问题标题】:How to set mendaroty Field * color for inputtextlayout hint?如何为 inputtextlayout 提示设置 mendaroty Field * 颜色?
【发布时间】:2017-10-21 22:41:23
【问题描述】:

这是我的 xml 文件。

<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <EditText
        android:id="@+id/input_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email*" />

</android.support.design.widget.TextInputLayout>

我需要红色的“*”。该怎么做??

【问题讨论】:

    标签: android material-components-android android-textinputlayout


    【解决方案1】:

    您可以使用材质组件库。
    从1.2.0 开始,您可以格式化提示文本。

    例如,你可以使用类似的东西:

    <com.google.android.material.textfield.TextInputLayout
        android:hint="@string/hint_test"
        ...>
    

    与:

    <string name="hint_test">Email <font fgcolor='#FF0000'>*</font></string>
    

    【讨论】:

      【解决方案2】:

      尝试将 HTML 添加到字符串中。类似的东西:

      <string name="mailHint">Email <span style="color: red">*</span></string>
      

      然后将其链接到您的提示:

      <EditText
              android:id="@+id/input_password"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="@string/mailHint" />
      

      【讨论】:

      • 如果它只是编辑文本但这里 EditText 在 TextInputLayout 中,它将起作用。所以在这里它不会工作。
      【解决方案3】:

      试试这个我的朋友,它会工作 100%

        SpannableStringBuilder builder = new SpannableStringBuilder();
      
          SpannableString str1= new SpannableString("Email");
          str1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorTemp)), 0, str1.length(), 0);
          builder.append(str1);
      
          SpannableString str2= new SpannableString("*");
          str2.setSpan(new ForegroundColorSpan(Color.RED), 0, str2.length(), 0);
          builder.append(str2);
      
          tvtv.setHint(builder);
      

      【讨论】:

        【解决方案4】:

        在 xml 的 EditText 中试试这个属性:

        android:drawableRight="@drawable/your_star_icon" 
        

        【讨论】:

          猜你喜欢
          • 2021-03-12
          • 1970-01-01
          • 2021-06-21
          • 2014-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多