【发布时间】:2011-12-06 22:09:45
【问题描述】:
我想为在edittext中输入的文本设置不同的提示字体和字体样式。 例如。假设提示字体大小为 12 及其正常类型。但是当用户开始输入编辑文本时,输入文本的字体大小应该变为 14 和粗体。再次,如果用户删除文本提示应该是上述类型。
【问题讨论】:
我想为在edittext中输入的文本设置不同的提示字体和字体样式。 例如。假设提示字体大小为 12 及其正常类型。但是当用户开始输入编辑文本时,输入文本的字体大小应该变为 14 和粗体。再次,如果用户删除文本提示应该是上述类型。
【问题讨论】:
您可以使用SpannableString 和MetricAffectingSpan 来实现它。您将能够更改提示的字体、大小和样式。
1) 创建自定义Hint 对象:
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.MetricAffectingSpan;
public class CustomHint extends SpannableString
{
public CustomHint(final CharSequence source, final int style)
{
this(null, source, style, null);
}
public CustomHint(final CharSequence source, final Float size)
{
this(null, source, size);
}
public CustomHint(final CharSequence source, final int style, final Float size)
{
this(null, source, style, size);
}
public CustomHint(final Typeface typeface, final CharSequence source, final int style)
{
this(typeface, source, style, null);
}
public CustomHint(final Typeface typeface, final CharSequence source, final Float size)
{
this(typeface, source, null, size);
}
public CustomHint(final Typeface typeface, final CharSequence source, final Integer style, final Float size)
{
super(source);
MetricAffectingSpan typefaceSpan = new CustomMetricAffectingSpan(typeface, style, size);
setSpan(typefaceSpan, 0, source.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
2) 创建自定义MetricAffectingSpan 对象:
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
public class CustomMetricAffectingSpan extends MetricAffectingSpan
{
private final Typeface _typeface;
private final Float _newSize;
private final Integer _newStyle;
public CustomMetricAffectingSpan(Float size)
{
this(null, null, size);
}
public CustomMetricAffectingSpan(Float size, Integer style)
{
this(null, style, size);
}
public CustomMetricAffectingSpan(Typeface type, Integer style, Float size)
{
this._typeface = type;
this._newStyle = style;
this._newSize = size;
}
@Override
public void updateDrawState(TextPaint ds)
{
applyNewSize(ds);
}
@Override
public void updateMeasureState(TextPaint paint)
{
applyNewSize(paint);
}
private void applyNewSize(TextPaint paint)
{
if (this._newStyle != null)
paint.setTypeface(Typeface.create(this._typeface, this._newStyle));
else
paint.setTypeface(this._typeface);
if (this._newSize != null)
paint.setTextSize(this._newSize);
}
}
3) 使用:
Typeface newTypeface = Typeface.createFromAsset(getAssets(), "AguafinaScript-Regular.ttf");
CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC, 60f);
// CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC);
// CustomHint customHint = new CustomHint(newTypeface, "Enter some text", 60f);
// CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC, 60f);
// CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC);
// CustomHint customHint = new CustomHint("Enter some text", 60f);
customEditText.setHint(customHint);
【讨论】:
已经给出的答案是正确的,但目前也可以通过 android:textColorHint 属性在 XML 文件中指定不同的颜色。例如,您可以这样做(假设您已将 my_favourite_colour 正确定义为资源):
<EditText
... other properties here ...
android:textColorHint="@color/my_favourite_colour"
</EditText>
【讨论】:
您可以使用以下代码以编程方式更改提示颜色,使其与在 edittext 中键入的字体样式不同
editTextId.setHintTextColor(Color.alpha(006666));
【讨论】: