EditView和TextView的用法差不多,只是文字可編輯
小技巧:
設定EditText隱藏鍵盤
(EditText)mMarket.setInputType(0);
設定EditText不被IME遮蓋
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
提示錯誤框(用於驗證必填等):
setError();
如果要對常值內容在代碼中加各種效果使用SpannableString
Android系統通過SpannableString類來對指定文本進行相關處理,具體有以下功能:
1、BackgroundColorSpan 背景色
SpannableString spanText = new SpannableString("背景文字");spanText.setSpan(new BackgroundColorSpan(Color.GREEN), 0, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
2、ClickableSpan 文本可點擊,有點擊事件
TextView tv = findViewById(R.id.tv_click);SpannableString spStr = new SpannableString("點擊的文字");ClickSpan clickSpan = new NoLineClickSpan(vo); //設定超連結spStr.setSpan(clickSpan, 0, str.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);tv.append(spStr);tv.setMovementMethod(LinkMovementMethod.getInstance());//設定超連結為可點擊狀態tv.setMovementMethod(LinkMovementMethod.getInstance());
3、ForegroundColorSpan 文本顏色(前景色彩)
spanText = new SpannableString("顏色文字");spanText.setSpan(new ForegroundColorSpan(Color.BLUE), 6, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
4、MaskFilterSpan 修飾效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
spanText = new SpannableString("MaskFilterSpan ");int length = spanText.length();//模糊(BlurMaskFilter)MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(3, Blur.OUTER));spanText.setSpan(maskFilterSpan, 0, length - 10, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//浮雕(EmbossMaskFilter)maskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{1,1,3}, 1.5f, 8, 3));spanText.setSpan(maskFilterSpan, length - 10, length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
5、MetricAffectingSpan 父類,一般不用
6、RasterizerSpan 光柵效果
spanText = new SpannableString("StrikethroughSpan");spanText.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
7、StrikethroughSpan 刪除線(中劃線)
spanText = new SpannableString("StrikethroughSpan");spanText.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
8、SuggestionSpan 相當於預留位置一般用在EditText輸入框中。當雙擊此文本時,會彈出提示框選擇一些建議(推薦的)文字,選中的文本將替換此預留位置。在IME上用的較多。
9、UnderlineSpan 底線
spanText = new SpannableString("UnderlineSpan");spanText.setSpan(new UnderlineSpan(), 0, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
10、AbsoluteSizeSpan 絕對大小(文本字型)
spanText = new SpannableString("AbsoluteSizeSpan");spanText.setSpan(new AbsoluteSizeSpan(20, true), 0, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
11、DynamicDrawableSpan 設定圖片,基於文本基準或底部對齊。
DynamicDrawableSpan drawableSpan = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE) { @Override public Drawable getDrawable() { Drawable d = getResources().getDrawable(R.drawable.ic_launcher); d.setBounds(0, 0, 50, 50); return d; }};DynamicDrawableSpan drawableSpan2 = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) { @Override public Drawable getDrawable() { Drawable d = getResources().getDrawable(R.drawable.ic_launcher); d.setBounds(0, 0, 50, 50); return d; } };spanText.setSpan(drawableSpan, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);spanText.setSpan(drawableSpan2, 7, 8, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
12、ImageSpan 圖片
spanText = new SpannableString("ImageSpan");Drawable d = getResources().getDrawable(R.drawable.ic_launcher);</strong>d.setBounds(0, 0, 50, 50);spanText.setSpan(new ImageSpan(d), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
13、RelativeSizeSpan 相對大小(文本字型)
spanText = new SpannableString("RelativeSizeSpan");//參數proportion:比例大小spanText.setSpan(new RelativeSizeSpan(2.5f), 3, 4,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
14、ReplacementSpan 父類,一般不用
15、ScaleXSpan 基於x軸縮放
spanText = new SpannableString("hello");//參數proportion:比例大小spanText.setSpan(new ScaleXSpan(3.8f), 3, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
16、StyleSpan 字型樣式:粗體、斜體等
spanText = new SpannableString("StyleSpan ");//Typeface.BOLD_ITALIC:粗體+斜體spanText.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 3, 7,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
17、SubscriptSpan 下標(數學公式會用到)
spanText = new SpannableString("SubscriptSpan ");spanText.setSpan(new SubscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
18、SuperscriptSpan 上標(數學公式會用到)
spanText = new SpannableString("SuperscriptSpan ");spanText.setSpan(new SuperscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
19、TextAppearanceSpan 文本外貌(包括字型、大小、樣式和顏色)
spanText = new SpannableString("TextAppearanceSpan");//若需自訂TextAppearance,可以在系統樣式上進行修改spanText.setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
20、TypefaceSpan 文本字型
spanText = new SpannableString("TypefaceSpan ");//若需使用自訂字型,可能要重寫類TypefaceSpanspanText.setSpan(new TypefaceSpan("monospace"), 3, 10,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);
21、URLSpan 文本超連結
spanText = new SpannableString("URLSpan ");spanText.setSpan(new URLSpan("http://orgcent.com"), 10, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);//讓URLSpan可以點擊mTVText.setMovementMethod(new LinkMovementMethod());