How can I set an EditText to have italic hint text?
How can I set the hint text of an EditText to be italic in Androi开发者_JAVA技巧d?
EditText.setText("");
EditText.setHint("hint");
EditText.setTypeface(Typeface.MONOSPACE, Typeface.ITALIC);
....
EditText.setText("text");
EditText.setTypeface(Typeface.MONOSPACE, Typeface.NORMAL);
To set text as italic, you can do the following:
EditText.setText(Html.fromHtml("<small><i>" + "Text Hint Here" + "</i></small>"));
If you would like the italic text to be a hint - when the EditText is empty for example - you must implement a method to check the text length, and when it is equal to zero, apply the above code.
You can do everything in XML.
First declare your hint in the "strings.xml" file. Your define it to be italic by using the "< i >" tag
<string name="EditBoxHint"><i>Whatever you want to show</i></string>
Finally, in the layout declaration you add your hint referencing this previously declared string.
<EditText
...
android:hint="@string/EditBoxHint"
...
/>
精彩评论