我在TextInputLayout内部有一个AppCompatEditText并从字符串资源中设置一个提示。
<string name="company_name">Companyname <font fgcolor='#FF0000'>*</font></string>星的颜色不会变成红色。
这就是我得到的。
我想要的(在photoshop中编辑)
我尝试使用带有图像的drawableEnd,但AppCompatEditText的宽度为“match_parent”。 所以它最后显示出来。 我不是这样的。 有没有办法做到这一点。
更新:
试过这个:
company_name.setHint(Html.fromHtml("<small><i>" + "Text Hint Here" + "</i><font color=#FF0000>*</font></small>"));这就是我得到的。 旧的提示不会被新的提示所取代。 两个提示都显示出来。
I have an AppCompatEditText Inside TextInputLayout and set a hint from string resource.
<string name="company_name">Companyname <font fgcolor='#FF0000'>*</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>*</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>"));
TextInputLayout上的Html/Spannable提示不起作用。
txt_usemail.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));
TextInputEditText上的Html/Spannable正在运行。
et_usemaill.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));
现在,问题在于如果你在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>"));
Html/Spannable on TextInputLayout hint is not working.
txt_usemail.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));
Html/Spannable on TextInputEditText is working.
et_usemaill.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));
Now, problem here is if you setHint() on TextInputEditText(like point 3) then Floating text won't work.
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 -
更多推荐
AppCompatEditText,company_name,font><,电脑培训,计算机培训,IT培训"/> <m
发布评论