Multiple hints or multiple style for the same hint in edittext

流过昼夜 提交于 2019-12-08 05:06:33

问题


How to add to an EditText component another hint that will react as an hint, but with different as the default hint.

For example: field name is city, so I would like the primary hint in black to be displayed as "city" and in light blue I would like it to be dislayed as "tap to select a city".

Are there any ready-to-go components or xml attributes for it? Is this a case where I need Java code and implement onfocus and show/hide another TextView that will react as label?


I got this as strings.xml:

<string name="fld_fullname">
    <![CDATA[
        <font color="#8B8B8B">City</font> <font color="#BABABA">tap to change</font>
    ]]>
</string>

and this in Java code:

etCity.setHint(Html.fromHtml(getString(R.string.fld_city)));

Now, how can I take the color from colors.xml? Might I use placeholders or is there a better way? Asking for best practice...


回答1:


There is only one hint attribute to EditText although you can add multi-colored hint programmatically like this

myEditView.setHint(Html.fromHtml("city - <font color='blue'>tap to select a city</font>."));

EDIT: if you are taking values from string.xml and color.xml then do following:

myEditView.setHint(Html.fromHtml(getString(R.string.hint1)+" <font color='"+getResources().getColor(R.color.hintColor)+"'>"+getString(R.string.hint2)+"</font>."));


来源:https://stackoverflow.com/questions/36758981/multiple-hints-or-multiple-style-for-the-same-hint-in-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!