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

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

问题描述

我有一个textinputlayout内的编辑文本。我想设置必填字段标志(红色星号)到的EditText。我已经设置了提示的编辑文本。我的code是如下图所示。

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外的星号。所以,当我开始在编辑文本输入,提示上浮,但不是星号。
我的要求是,以星号来作为EditText上的提示,同样的行为。什么是最好的办法?
任何帮助是AP preciated。在此先感谢

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

推荐答案

您可以做到这一点使用一个小窍门,即你必须改变 layout_gravity OnFocusChangeListener 称为Java中code。例如:

You can do this using a small trick , i.e you have to change layout_gravity when OnFocusChangeListener called in Java code. Example:

//Set OnFocusChangeListener for EditText
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                //astriskText is Object of your textview contains * as text
                if (hasFocus) {
                    //if edittext has focus then move it to top
                    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) astriskText.getLayoutParams();
                    lp.gravity= Gravity.TOP;
                    astriskText.setLayoutParams(lp);
                } else if(edittext.getText().toString().length()==0){
                    //else check if edittext is empty then move it back to center
                    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) astriskText.getLayoutParams();
                    lp.gravity= Gravity.CENTER_VERTICAL;
                    astriskText.setLayoutParams(lp);
                }
            }
        });

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

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