Multiple hints or multiple style for the same hint

2019-06-06 16:08发布

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条回答
戒情不戒烟
2楼-- · 2019-06-06 16:45

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>."));
查看更多
登录 后发表回答