Android TextView前增加紅色必填項星號*的示例代碼
TextView是什么
向用戶顯示文本,并可選擇允許他們編輯文本。TextView是一個完整的文本編輯器,但是基類為不允許編輯;其子類EditText允許文本編輯。
咱們先上一個圖看看TextView的繼承關(guān)系:

從上圖可以看出TxtView繼承了View,它還是Button、EditText等多個組件類的父類。咱們看看這些子類是干嘛的。
- Button:用戶可以點擊或單擊以執(zhí)行操作的用戶界面元素。
- CheckedTextView:TextView支持Checkable界面和顯示的擴(kuò)展。
- Chronometer:實現(xiàn)簡單計時器的類。
- DigitalClock:API17已棄用可用TextClock替代。
- EditText:用于輸入和修改文本的用戶界面元素。
- TextClock:可以將當(dāng)前日期和/或時間顯示為格式化字符串。
下面來看看Android TextView前增加紅色必填項星號*
自定義屬性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NecessaryTextView">
<attr name="necessary" format="boolean" />
</declare-styleable>
</resources>自定義控件
import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
// TextView that start with a red char of *
class NecessaryTextView : AppCompatTextView {
private var necessary = false
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.NecessaryTextView)
necessary = typedArray.getBoolean(R.styleable.NecessaryTextView_necessary, false)
typedArray.recycle()
setText(text, null)
}
override fun setText(text: CharSequence?, type: BufferType?) {
if (necessary) {
val span = SpannableString("*$text")
span.setSpan(ForegroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
super.setText(span, type)
} else {
super.setText(text, type)
}
}
}到此這篇關(guān)于Android TextView前增加紅色必填項星號*的示例代碼的文章就介紹到這了,更多相關(guān)Android TextView必填項星號*內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android OpenGL入門之GLSurfaceView
這篇文章主要介紹了OpenGL入門知識,如何在Android中使用GLSurfaceView,如果對OpenGL感興趣的同學(xué),可以參考下2021-04-04
Android使用AudioManager修改系統(tǒng)音量的方法
這篇文章主要介紹了Android使用AudioManager修改系統(tǒng)音量的方法,結(jié)合實例形式分析了AudioManager調(diào)節(jié)音量的常用方法及相關(guān)使用技巧,需要的朋友可以參考下2016-08-08
Android scheme 跳轉(zhuǎn)的設(shè)計與實現(xiàn)詳解
這篇文章主要介紹了Android scheme 跳轉(zhuǎn)的設(shè)計與實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
android中soap協(xié)議使用(ksoap調(diào)用webservice)
kSOAP是如何調(diào)用ebservice的呢,首先要使用SoapObject,這是一個高度抽象化的類,完成SOAP調(diào)用。可以調(diào)用它的addProperty方法填寫要調(diào)用的webservice方法的參數(shù)2014-02-02
android自定義控件創(chuàng)建翻頁接口詳細(xì)代碼
這篇文章主要為大家介紹了android自定義控件創(chuàng)建翻頁接口詳細(xì)代碼,具有一定的實用性,感興趣的小伙伴們可以參考一下2016-07-07

