首先新建一个类继承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, boolean isRequired) {
if(isRequired) {
Spannable span = new SpannableString(prefix + text);
span.setSpan(new ForegroundColorSpan(prefixColor), 0, prefix.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(span);
} else setText(text);
}
}
以上代码理解起来不难,主要是在初始化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.qyspace.ui.widget.RequiredTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="名称:" />
<com.qyspace.ui.widget.RequiredTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年:"
app:prefix="++"
app:prefix_color="#FF6100" />
</LinearLayout>