欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android TextView前增加紅色必填項星號*的示例代碼

 更新時間:2024年03月18日 15:38:45   作者:命運之手  
TextView是一個完整的文本編輯器,但是基類為不允許編輯,其子類EditText允許文本編輯,這篇文章主要介紹了Android TextView前增加紅色必填項星號*的示例代碼,需要的朋友可以參考下

TextView是什么

        向用戶顯示文本,并可選擇允許他們編輯文本。TextView是一個完整的文本編輯器,但是基類為不允許編輯;其子類EditText允許文本編輯。

        咱們先上一個圖看看TextView的繼承關(guān)系:

        從上圖可以看出TxtView繼承了View,它還是Button、EditText等多個組件類的父類。咱們看看這些子類是干嘛的。

  • Button:用戶可以點擊或單擊以執(zhí)行操作的用戶界面元素。
  • CheckedTextView:TextView支持Checkable界面和顯示的擴展。
  • 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)文章

最新評論