我想爲不同的字體樣式提示和字體樣式在edittext文本中輸入。 例如。可以說,提示字體大小是12和它的正常類型。但是當用戶開始在edittext中輸入時,輸入文本的字體大小應該變爲14並且粗體。再次如果用戶刪除文本提示應該是上述類型。不同的提示字體樣式和鍵入的文本字體樣式android
6
A
回答
4
您可以以編程方式更改提示顏色,使其通過使用下面的代碼
editTextId.setHintTextColor(Color.alpha(006666));
0
已經給出的答案是正確的,但目前指定不同的顏色可能也是在在EditText上輸入字體樣式不同XML文件通過android:textColorHint屬性。舉例來說,你可以做這樣的事情(假設你已經正確定義my_favourite_colour作爲一種資源):
<EditText
... other properties here ...
android:textColorHint="@color/my_favourite_colour"
</EditText>
0
可以使用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);
相關問題
- 1. 嵌入式字體樣式 - 嵌入式字體風格VS CSS樣式
- 2. 提示的背景顏色變化和字體樣式primefaces提示文本的
- 3. 字體樣式@字體面不與本地字體工作
- 4. Tkinter字體樣式
- 5. 已下載有不同字體樣式的字體
- 6. 自定義字體和粗體樣式的文本
- 7. sifr 3.0 - 字體樣式不顯示?
- 8. SVG動畫文本字體樣式
- 9. 爲什麼字體樣式不能去除'斜體'樣式?
- 10. JTextComponent中的多種字體和樣式
- 11. Android的 - 多種字體樣式,組合成一個單一的字體(字體)
- 12. 更改字體樣式
- 13. 外部字體樣式表
- 14. Windows Phone 7字體樣式
- 15. Xamarin Forms Android:TabbedPage字體樣式
- 16. toolStripComboBox設置字體樣式?
- 17. Angularjs更改字體樣式
- 18. 字體樣式dropdownlist asp.net
- 19. 使用'特別'字體的樣式不是HTML或CSS字體樣式(谷歌字體)
- 20. Flash Builder和MXMLC的Flex應用程序字體樣式不同
- 21. 使用不同文本和字體樣式的多個H3標籤
- 22. ACE編輯器字體樣式騎過我現有的字體樣式
- 23. 如何檢查Android Espresso中的文字字體樣式
- 24. 樣式化的Unicode箭頭顯示不與谷歌網頁字體的字體
- 25. CSS樣式表和自定義字體
- 26. 更改字體大小和樣式
- 27. 添加樣式後字體樣式不變
- 28. InDesign腳本更改文本框中的子字體樣式
- 29. 在C#GUI中更改字體大小和字體樣式
- 30. iOS - 安全文本輸入會導致字體大小和最後一個字符的樣式不同