Android自定义TextView实现必填项前面的*号

首先新建一个类继承TextView,然后重写setText,代码如下:

    @Override
    public void setText(CharSequence text, BufferType type) {
        Spannable span = new SpannableString("*" + text);
        span.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        super.setText(span, type);
    }

xml布局部分代码如下:

 <com.example.widget.RequiredTextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="姓名:" />

效果如下所示:

但这种方法有一定的局限性,如果有一些TextView的必填项前缀不是*,颜色也不是红色,那就需要重新自定义TextView。略有点繁琐,所以可以将前缀跟颜色提取出来作为自定义属性来使用,下面使用另外一种方法来看一下。

首先在res目录下的values目录里新建一个xml文件,取名为attrs.xml,代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RequiredTextView">
        <attr name="android:text" />
        <attr name="prefix" format="string|reference" />
        <attr name="prefix_color" format="color|reference" />
    </declare-styleable>
</resources>

然后新建一个类继承TextView,代码如下所示:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.TextView;


public class RequiredTextView extends TextView {

    private String prefix = "*";
    private int prefixColor = Color.RED;

    public RequiredTextView(Context context) {
        super(context);
    }

    public RequiredTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RequiredTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, @Nullable AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RequiredTextView);

        prefix = ta.getString(R.styleable.RequiredTextView_prefix);
        prefixColor = ta.getInteger(R.styleable.RequiredTextView_prefix_color, Color.RED);
        String text = ta.getString(R.styleable.RequiredTextView_android_text);
        if (TextUtils.isEmpty(prefix)) {
            prefix = "*";
        }
        if (TextUtils.isEmpty(text)) {
            text = "";
        }
        ta.recycle();
        setText(text);
    }

    public void setText(String text) {
        Spannable span = new SpannableString(prefix + text);
        span.setSpan(new ForegroundColorSpan(prefixColor), 0, prefix.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        setText(span);
    }

}

以上代码理解起来不难,主要是在初始化TextView的时候取出前缀属性跟前缀颜色属性,然后使用Spannable设置前缀。activity对应的布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">

    <com.huishang.chauffeur.ui.widget.RequiredTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名:" />

    <com.huishang.chauffeur.ui.widget.RequiredTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="年龄:"
        app:prefix="++"
        app:prefix_color="#FF6100" />
</LinearLayout>

上面是默认的前缀跟颜色,下面是自定义的前缀跟颜色,效果如下图所示:

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值