Android 自定义Edittext 和TextView 提示文字和填入内容不同的粗细组件

近期项目中又EditText 以及TextView 这两个组件需要用到提示文字 以及 填入文字要保持不同的粗细程度,所以记录一下

首先 是EditText 组件的自定义

BLEditText 继承的这个组件是一个三方的组件,可以在很大程度上减少drawable的编写,有兴趣的可以去相关的Git去看一下 点击查看,也可以直接继承Edittext,使用的时候直接调用即可

java 复制代码
class AutoBoldEditText : BLEditText {
    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defstyleAttr: Int) : super(
        context,
        attrs,
        defstyleAttr
    ) {
        init()
    }

    private fun init() {
        //动态设置内容的粗细
        addTextChangedListener {
            typeface = if (it.toString().isEmpty()) {
                Typeface.DEFAULT
            } else {
                Typeface.DEFAULT_BOLD
            }
        }
    }
}

下面是TextView的组件

同理,继承也可以直接继承TextView

java 复制代码
class AutoBoldTextView(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
    BLTextView(context, attrs, defStyleAttr) {
        init {
            addTextChangedListener {
                if (it.toString().isEmpty()){
                    typeface = Typeface.DEFAULT
                }else {
                    typeface = Typeface.DEFAULT_BOLD
                }
            }

        }
}

做个记录,有帮助的话,点个赞意思一下呗

相关推荐
oMcLin2 分钟前
如何在 RHEL 8 系统上实现高可用 MySQL 集群,保障电商平台的 24 小时稳定运行
android·mysql·adb
踏雪羽翼22 分钟前
Android 应用冷启动优化
android·性能优化·性能·启动·冷启动·应用冷启动
2301_7873284943 分钟前
44.Python(二)
android·python
峥嵘life2 小时前
Android16 EDLA 【CTS-V】Host-side 存在fail
android·linux·学习
_李小白2 小时前
【Android 性能分析工具】第二天:认识Perfetto
android
程序漫游人2 小时前
苹果IOS App Store加快审核进度
android·ios·软件工程·iphone
带带弟弟学爬虫__2 小时前
fancygo 解密演示
android·python·算法·网络爬虫
我命由我123452 小时前
Android 控件 - 悬浮常驻文本交互(IBinder 实现、BroadcastReceiver 实现)
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
Android-Flutter3 小时前
android compose 左右滑动带指示器,HorizontalPager和HorizontalPagerIndicator使用
android·kotlin
csj503 小时前
安卓基础之《(13)—数据存储(3)存储卡的文件操作》
android