这有可能改变android中提示的一部分颜色吗?(Is this possible to change a portion of hint's color in android?)

编程入门 行业动态 更新时间:2024-10-26 16:29:33
这有可能改变android中提示的一部分颜色吗?(Is this possible to change a portion of hint's color in android?)

我在TextInputLayout内部有一个AppCompatEditText并从字符串资源中设置一个提示。

<string name="company_name">Companyname <font fgcolor='#FF0000'>&#42;</font></string>

星的颜色不会变成红色。

这就是我得到的。

我想要的(在photoshop中编辑)

我尝试使用带有图像的drawableEnd,但AppCompatEditText的宽度为“match_parent”。 所以它最后显示出来。 我不是这样的。 有没有办法做到这一点。

更新:

试过这个:

company_name.setHint(Html.fromHtml("<small><i>" + "Text Hint Here" + "</i><font color=#FF0000>&#42;</font></small>"));

这就是我得到的。 旧的提示不会被新的提示所取代。 两个提示都显示出来。

I have an AppCompatEditText Inside TextInputLayout and set a hint from string resource.

<string name="company_name">Companyname <font fgcolor='#FF0000'>&#42;</font></string>

The color of the star is not changing to red.

This is what What I got.

What I want (edited in photoshop)

I tried drawableEnd with an image, but the AppCompatEditText has the width as "match_parent". So its displayed at the end. I don't what that way. Is there any way to do it.

UPDATE :

Tried this:

company_name.setHint(Html.fromHtml("<small><i>" + "Text Hint Here" + "</i><font color=#FF0000>&#42;</font></small>"));

This is what I got. The old hint is not replaced by new one. Both hints are showing.

最满意答案

在某种程度上......

这是我的发现

TextInputEditText Floating文本上的Html/Spannable无法正常工作。

txt_usemail.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));

1

TextInputLayout上的Html/Spannable提示不起作用。

txt_usemail.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));

2

TextInputEditText上的Html/Spannable正在运行。

et_usemaill.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));

3

现在,问题在于如果你在TextInputEditText上设置了setHint() (如第3点),那么Floating文本就不起作用了。

在此处输入图像描述

要使用Floating文本,我们需要在TextInputLayout上设置setHint()

这是使用EditText焦点/散焦的技巧

final TextInputLayout txt_usemail = findViewById(R.id.txt_usemail); final TextInputEditText et_usemaill = findViewById(R.id.et_usemaill); et_usemaill.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>")); et_usemaill.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean hasFocus) { if (et_usemaill.getText().toString().equals("")) { if (hasFocus) { et_usemaill.setHint(null); txt_usemail.setHint(Html.fromHtml("<font color=\"#c5c5c5\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>")); } else { // Remove Glitch/Smooth Animation new Handler().postDelayed(new Runnable() { @Override public void run() { txt_usemail.setHint(null); et_usemaill.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>")); } }, 200); } } } });

样式

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorControlNormal">#c5c5c5</item> <item name="colorControlActivated">#c5c5c5</item> <item name="colorControlHighlight">#c5c5c5</item> </style> <style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance"> <item name="android:textColor">@android:color/holo_red_dark</item> <item name="android:textSize">12sp</item> </style>

布局

<android.support.design.widget.TextInputLayout android:layout_marginTop="56dp" android:id="@+id/txt_usemail" android:layout_width="match_parent" android:layout_height="wrap_content" app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout" android:textColorHint="@android:color/holo_red_dark" > <android.support.design.widget.TextInputEditText android:id="@+id/et_usemaill" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:inputType="textEmailAddress" android:textColor="@color/white" /> </android.support.design.widget.TextInputLayout>

结果 -

在此处输入图像描述

To some extent possible ...

here are my findings

Html/Spannable on Floating text of TextInputEditText is not working.

txt_usemail.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));

1

Html/Spannable on TextInputLayout hint is not working.

txt_usemail.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));

2

Html/Spannable on TextInputEditText is working.

et_usemaill.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));

3

Now, problem here is if you setHint() on TextInputEditText(like point 3) then Floating text won't work.

enter image description here

to use Floating text, we need to setHint() on TextInputLayout

Here is the trick using EditText focus/defocus

final TextInputLayout txt_usemail = findViewById(R.id.txt_usemail); final TextInputEditText et_usemaill = findViewById(R.id.et_usemaill); et_usemaill.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>")); et_usemaill.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean hasFocus) { if (et_usemaill.getText().toString().equals("")) { if (hasFocus) { et_usemaill.setHint(null); txt_usemail.setHint(Html.fromHtml("<font color=\"#c5c5c5\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>")); } else { // Remove Glitch/Smooth Animation new Handler().postDelayed(new Runnable() { @Override public void run() { txt_usemail.setHint(null); et_usemaill.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>")); } }, 200); } } } });

style

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorControlNormal">#c5c5c5</item> <item name="colorControlActivated">#c5c5c5</item> <item name="colorControlHighlight">#c5c5c5</item> </style> <style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance"> <item name="android:textColor">@android:color/holo_red_dark</item> <item name="android:textSize">12sp</item> </style>

layout

<android.support.design.widget.TextInputLayout android:layout_marginTop="56dp" android:id="@+id/txt_usemail" android:layout_width="match_parent" android:layout_height="wrap_content" app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout" android:textColorHint="@android:color/holo_red_dark" > <android.support.design.widget.TextInputEditText android:id="@+id/et_usemaill" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:inputType="textEmailAddress" android:textColor="@color/white" /> </android.support.design.widget.TextInputLayout>

Result -

enter image description here

更多推荐

AppCompatEditText,company_name,font><,电脑培训,计算机培训,IT培训"/> <m

本文发布于:2023-07-15 05:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110685.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这有   颜色   提示   android   color

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!