对 textinputlayout 内的编辑文本(红色星号)具有强制性符号 [英] Having the mandatory symbol to the edit text (red color asterisk) which is inside the textinputlayout

查看:25
本文介绍了对 textinputlayout 内的编辑文本(红色星号)具有强制性符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 textinputlayout 中有一个编辑文本.我正在尝试将必填字段符号(红色星号)设置为编辑文本.我已将提示设置为编辑文本.我的代码如下.

I have an edit text inside a textinputlayout. I am trying to set the mandatory field symbol (red color asterisk) to the edittext. I have set the hint to the edit text. My code is as below.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text=" * "
            android:layout_gravity="center_vertical"

            android:textColor="#ff0000"
            android:textSize="15sp" />
        <android.support.design.widget.TextInputLayout

            android:id="@+id/tilEngNo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >

              <EditText
                android:id="@+id/engineno"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="15dp"
            android:layout_marginRight="16dp"
                android:hint="Engine No" />
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

这给了我 textinputlayout 之外的星号符号.因此,当我开始输入编辑文本时,提示会浮动,但星号符号不会.我的要求是使星号符号的行为与编辑文本的提示相同.最好的方法是什么?任何帮助表示赞赏.提前致谢

This gives me the asterisk symbol outside the textinputlayout. So when I start typing in the edit text, the hint floats up, but not the asterisk symbol. My requirement is to make the asterisk symbol to behave as same as the hint of the edittext. What is the best approach?. Any help is appreciated. Thanks in advance

推荐答案

假设您有 EditTexts 用于强制值.您希望提示为红色星号,后跟浅灰色文本.

Suppose you have EditTexts for mandatory values. You want the hint to be a red asterisk followed by the text in light grey.

在 EditTexts 之后,您有解释:带有红色星号的 TextView 后跟文本= 字段是必需的"

After the EditTexts you have the explanation: TextView with red asterisk followed by text " = field is required"

就我而言,我使用了这个:

In my case I used this this:

showEditTextsAsMandatory ( joinEmailET, joinNameET, passwordET, confirmPasswordET );
showTextViewsAsMandatory ( ( TextView ) findViewById ( R.id.requiredText ) );

public void showEditTextsAsMandatory ( EditText... ets )
{
    for ( EditText et : ets )
    {
        String hint = et.getHint ().toString ();

        et.setHint ( Html.fromHtml ( "<font color="#ff0000">" + "* " + "</font>" + hint ) );
    }
}

public void showTextViewsAsMandatory ( TextView... tvs )
{
    for ( TextView tv : tvs )
    {
        String text = tv.getText ().toString ();

        tv.setText ( Html.fromHtml ( "<font color="#ff0000">" + "* " + "</font>" + text ) );
    }
}

这篇关于对 textinputlayout 内的编辑文本(红色星号)具有强制性符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆