edt = (EditText) findViewById(R.id.edt);
SpannableString string = new SpannableString("这是EditText的提示文案");
//第一个属性代表提示文案的字体大小,第二个参数boolean dip如果为true,表示前面的字体大小单位为dip,否则为像素
AbsoluteSizeSpan span = new AbsoluteSizeSpan(8,true);
string.setSpan(span, 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
edt.setHint(string);
以上代码就可以实现我们想要的效果了!
SpannableString还可以设置:
以下的b是你要设置字体样式的那段文本
SpannableString spannableString = new SpannableString(b);
1、设置字体
spannableString.setSpan(new TypefaceSpan("default"), 0, b.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new TypefaceSpan("default-bold"), 0, b.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new TypefaceSpan("monospace"), 0, b.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new TypefaceSpan("serif"), 0, b.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new TypefaceSpan("sans-serif"), 0, b.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2、设置字体大小(绝对值、单位:像素)
spannableString.setSpan(new AbsoluteSizeSpan(50), 0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new AbsoluteSizeSpan(20,true), 0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
3、设置字体前景色
spannableString.setSpan(new ForegroundColorSpan(Color.RED),0,b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
4、设置字体背景色
spannableString.setSpan(new BackgroundColorSpan(Color.CYAN), 0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
5、设置字体样式:normal正常 bold粗体 italic斜体 bold_italic粗斜体
spannableString.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL),0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
6、设置下划线
spannableString.setSpan(new UnderlineSpan(),0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
7、设置删除线
spannableString.setSpan(new StrikethroughSpan(),0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
8、设置图片(指定图片显示区间内的文字替换成图片)
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
spannableString.setSpan(new ImageSpan(drawable), 0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
9、电话
spannableString.setSpan(new URLSpan("tel:10086"), 0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
10、邮件
spannableString.setSpan(new URLSpan("[email protected]"), 0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
11、网络
spannableString.setSpan(new URLSpan("http://www.baidu.com"), 0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
12、短信
spannableString.setSpan(new URLSpan("sms10086"), 0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
13、彩信
spannableString.setSpan(new URLSpan("mms10086"), 0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
14、地图
spannableString.setSpan(new URLSpan("geo:32.123456,-17.123456"), 0, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//无论你要设置什么样式的文字都要写如下代码,否则无效(tv为你要设置的控件)
tv.setText(spannableString);
tv.setMovementMethod(LinkMovementMethod.getInstance());