textview是什么
向用户显示文本,并可选择允许他们编辑文本。textview是一个完整的文本编辑器,但是基类为不允许编辑;其子类edittext允许文本编辑。
咱们先上一个图看看textview的继承关系:
从上图可以看出txtview继承了view,它还是button、edittext等多个组件类的父类。咱们看看这些子类是干嘛的。
- button:用户可以点击或单击以执行操作的用户界面元素。
- checkedtextview:textview支持checkable界面和显示的扩展。
- chronometer:实现简单计时器的类。
- digitalclock:api17已弃用可用textclock替代。
- edittext:用于输入和修改文本的用户界面元素。
- textclock:可以将当前日期和/或时间显示为格式化字符串。
下面来看看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) } } }
到此这篇关于android textview前增加红色必填项星号*的示例代码的文章就介绍到这了,更多相关android textview必填项星号*内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论