TextInputLayout和EditText双提示问题

时间:2021-12-13 15:59:19

I want to set the hint with java in EditText(which is in TextInputLayout).

我想在EditText(在TextInputLayout中)中使用java设置提示。

Code used for setting hint:

用于设置提示的代码:

aET = (EditText) findViewById(R.id.aET); aET.setHint("h?");

aET =(EditText)findViewById(R.id.aET); aET.setHint( “H?”);

But even when edittext is focused, Hint is displayed twice(inside edittext also).

但是即使在关注edittext时,Hint也会显示两次(也在edittext内)。

Please let me know if anyone had faced and found some workaround

如果有人面对并找到一些解决方法,请告诉我

when editText is focused...

当editText聚焦时......

TextInputLayout和EditText双提示问题

when editText is not focused..

当editText没有集中时..

TextInputLayout和EditText双提示问题

EDIT[10th July 2015]:

编辑[2015年7月10日]:

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

6 个解决方案

#1


9  

I found the solution !

我找到了解决方案!

In your EditText add

在EditText中添加

android:textColorHint="@android:color/transparent"

And in the code set the hint from the EditText

并在代码中设置EditText的提示

aET.setHint("h?");

The hint in your editText is hidden and the hint from the TextInputLayout is shown.

您的editText中的提示将被隐藏,并显示TextInputLayout中的提示。

EDIT :

编辑:

Other solution (The best)

其他解决方案(最好的)

Update Graddle with the new version of android:design

使用新版本的android:design更新Graddle

compile 'com.android.support:design:22.2.1'

#2


36  

This problem occurs because the hint from the xml is passed on to the TextInputLayout, so it can be displayed as a floating hint. But when you set it programatically, the hint is set to the EditText, which then results in two hints (one from the xml that is a floating hint and one you set programatically and is a normal EditText hint). If you wish to change the floating hint, you must set the hint on TextInputLayout.

出现此问题的原因是xml中的提示传递给TextInputLayout,因此它可以显示为浮动提示。但是当你以编程方式设置它时,提示被设置为EditText,然后产生两个提示(一个来自xml,它是一个浮动提示,一个是以编程方式设置的,是一个正常的EditText提示)。如果要更改浮动提示,则必须在TextInputLayout上设置提示。

You code will then look like this:

您的代码将如下所示:

aTIL = (TextInputLayout) findViewById(R.id.aTIL);
aTIL.setHint("h?");

#3


10  

I also had this issue.

我也有这个问题。

when I needed to update the hint, I (erroneously) did that on both the EditText and the TextInputLayout, which resulted in a blur.

当我需要更新提示时,我(错误地)在EditText和TextInputLayout上都做了这个,这导致了模糊。

solution was to not call EditText.setHint() but only TextInputLayout.setHint()

解决方案是不调用EditText.setHint()而只调用TextInputLayout.setHint()

#4


1  

first compile dependency compile 'com.rengwuxian.materialedittext:library:2.1.3'

首先编译依赖编译'com.rengwuxian.materialedittext:library:2.1.3'

add this in your xml

在你的xml中添加它

<com.rengwuxian.materialedittext.MaterialEditText    
android:id="@+id/etId"    
android:layout_width="match_parent"    
android:layout_height="70dip"    
android:hint="Hint goes here"    
android:textColor = "#000000"    
android:textSize="15sp"    
app:met_accentTypeface="fonts/yourcustomfonts.ttf"   
app:met_floatingLabel="normal"    
app:met_floatingLabelTextColor="#ff0000"    
app:met_floatingLabelTextSize="15sp"    
app:met_hideUnderline="true"    
app:met_textColorHint="#ff00ff"    
app:met_typeface="fonts/yourcustomfont.ttf"/>

add xmlns:app="http://schemas.android.com/apk/res-auto"

添加xmlns:app =“http://schemas.android.com/apk/res-auto”

#5


0  

Simple use this programmatically it's working for me

以编程方式简单地使用它对我有用

    TextInputLayout til = new TextInputLayout(this);
    til.setHintTextAppearance(android.R.style.TextAppearance_Large);

You can customize the style as i mention below...

您可以自定义下面提到的样式......

<style name="TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/colorPrimaryDark</item>
        <item name="android:textColorHint">@color/colorPrimaryDark</item>
        <item name="android:textSize">23sp</item>
    </style>

TextInputLayout til = new TextInputLayout(this);
        til.setHint("hai);
        til.setHintTextAppearance(R.style.TextInputLayout);

#6


0  

You can go by using two hint texts. One for the TextInputLayout and other for the edit text inside it.Below is the reference:

您可以使用两个提示文本。一个用于TextInputLayout,另一个用于其中的编辑文本.Below是引用:

<android.support.design.widget.TextInputLayout
                                    style="@style/editText_layout"
                                    android:id="@+id/entryScreenProduction_editText_percentCompleted_layout"
                                    android:hint="%Completed">

                                    <EditText
                                        android:id="@+id/entryScreenProduction_editText_percentCompleted"
                                        style="@style/editTextNotes"
                                        android:gravity="center_vertical|top"
                                        android:inputType="numberDecimal"
                                        android:textAlignment="viewStart"
                                        android:hint="0.0"/>
</android.support.design.widget.TextInputLayout>

But this has a problem. The UI will look like below. The hints will overlap. TextInputLayout和EditText双提示问题

但这有一个问题。用户界面如下所示。提示将重叠。

To avoid the above ugly UI, you have to control this from program.Set the hint text of the EditText only when there is focus on the field, else remove the hint text.

为了避免上面丑陋的UI,你必须从program.Sto只有当字段有焦点时才设置EditText的提示文本,否则删除提示文本。

android:hint="0.0"

Remove the above line from the layout xml file and do as below:

从布局xml文件中删除上面的行,并执行以下操作:

m_editText_completed = (EditText) findViewById(R.id.entryScreenProduction_editText_percentCompleted);
m_editText_completed.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    m_editText_completed.setHint("0.0");
                }
                else {
                    m_editText_completed.setHint("");
                }
            }
        });

I hope this solves you issue. God Speed !!!

我希望这能解决你的问题。神速!!!

#1


9  

I found the solution !

我找到了解决方案!

In your EditText add

在EditText中添加

android:textColorHint="@android:color/transparent"

And in the code set the hint from the EditText

并在代码中设置EditText的提示

aET.setHint("h?");

The hint in your editText is hidden and the hint from the TextInputLayout is shown.

您的editText中的提示将被隐藏,并显示TextInputLayout中的提示。

EDIT :

编辑:

Other solution (The best)

其他解决方案(最好的)

Update Graddle with the new version of android:design

使用新版本的android:design更新Graddle

compile 'com.android.support:design:22.2.1'

#2


36  

This problem occurs because the hint from the xml is passed on to the TextInputLayout, so it can be displayed as a floating hint. But when you set it programatically, the hint is set to the EditText, which then results in two hints (one from the xml that is a floating hint and one you set programatically and is a normal EditText hint). If you wish to change the floating hint, you must set the hint on TextInputLayout.

出现此问题的原因是xml中的提示传递给TextInputLayout,因此它可以显示为浮动提示。但是当你以编程方式设置它时,提示被设置为EditText,然后产生两个提示(一个来自xml,它是一个浮动提示,一个是以编程方式设置的,是一个正常的EditText提示)。如果要更改浮动提示,则必须在TextInputLayout上设置提示。

You code will then look like this:

您的代码将如下所示:

aTIL = (TextInputLayout) findViewById(R.id.aTIL);
aTIL.setHint("h?");

#3


10  

I also had this issue.

我也有这个问题。

when I needed to update the hint, I (erroneously) did that on both the EditText and the TextInputLayout, which resulted in a blur.

当我需要更新提示时,我(错误地)在EditText和TextInputLayout上都做了这个,这导致了模糊。

solution was to not call EditText.setHint() but only TextInputLayout.setHint()

解决方案是不调用EditText.setHint()而只调用TextInputLayout.setHint()

#4


1  

first compile dependency compile 'com.rengwuxian.materialedittext:library:2.1.3'

首先编译依赖编译'com.rengwuxian.materialedittext:library:2.1.3'

add this in your xml

在你的xml中添加它

<com.rengwuxian.materialedittext.MaterialEditText    
android:id="@+id/etId"    
android:layout_width="match_parent"    
android:layout_height="70dip"    
android:hint="Hint goes here"    
android:textColor = "#000000"    
android:textSize="15sp"    
app:met_accentTypeface="fonts/yourcustomfonts.ttf"   
app:met_floatingLabel="normal"    
app:met_floatingLabelTextColor="#ff0000"    
app:met_floatingLabelTextSize="15sp"    
app:met_hideUnderline="true"    
app:met_textColorHint="#ff00ff"    
app:met_typeface="fonts/yourcustomfont.ttf"/>

add xmlns:app="http://schemas.android.com/apk/res-auto"

添加xmlns:app =“http://schemas.android.com/apk/res-auto”

#5


0  

Simple use this programmatically it's working for me

以编程方式简单地使用它对我有用

    TextInputLayout til = new TextInputLayout(this);
    til.setHintTextAppearance(android.R.style.TextAppearance_Large);

You can customize the style as i mention below...

您可以自定义下面提到的样式......

<style name="TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/colorPrimaryDark</item>
        <item name="android:textColorHint">@color/colorPrimaryDark</item>
        <item name="android:textSize">23sp</item>
    </style>

TextInputLayout til = new TextInputLayout(this);
        til.setHint("hai);
        til.setHintTextAppearance(R.style.TextInputLayout);

#6


0  

You can go by using two hint texts. One for the TextInputLayout and other for the edit text inside it.Below is the reference:

您可以使用两个提示文本。一个用于TextInputLayout,另一个用于其中的编辑文本.Below是引用:

<android.support.design.widget.TextInputLayout
                                    style="@style/editText_layout"
                                    android:id="@+id/entryScreenProduction_editText_percentCompleted_layout"
                                    android:hint="%Completed">

                                    <EditText
                                        android:id="@+id/entryScreenProduction_editText_percentCompleted"
                                        style="@style/editTextNotes"
                                        android:gravity="center_vertical|top"
                                        android:inputType="numberDecimal"
                                        android:textAlignment="viewStart"
                                        android:hint="0.0"/>
</android.support.design.widget.TextInputLayout>

But this has a problem. The UI will look like below. The hints will overlap. TextInputLayout和EditText双提示问题

但这有一个问题。用户界面如下所示。提示将重叠。

To avoid the above ugly UI, you have to control this from program.Set the hint text of the EditText only when there is focus on the field, else remove the hint text.

为了避免上面丑陋的UI,你必须从program.Sto只有当字段有焦点时才设置EditText的提示文本,否则删除提示文本。

android:hint="0.0"

Remove the above line from the layout xml file and do as below:

从布局xml文件中删除上面的行,并执行以下操作:

m_editText_completed = (EditText) findViewById(R.id.entryScreenProduction_editText_percentCompleted);
m_editText_completed.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    m_editText_completed.setHint("0.0");
                }
                else {
                    m_editText_completed.setHint("");
                }
            }
        });

I hope this solves you issue. God Speed !!!

我希望这能解决你的问题。神速!!!