如何自定义 SDK 中文本内容超链接

1. 继承 sdk 中的AutoLinkTextView 类,覆盖setText方法

public class TestTextView extends AutoLinkTextView {
    public TestTextView(Context context) {
        super(context);
    }

    public TestTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TestTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TestTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setAutoLinkMask(0);
        super.setText(text, type);
        RongLinkify.addLinks(this, RongLinkify.ALL);
    }
}

2. 参考RongLinkify类,实现你们自己的link处理逻辑。 RongLinkify类已经上传:

private static final String WEB_URL_REGEX = “[-a-zA-Z0-9+&@#/?=~\_|!:.,;\]\*\\\\.\[-a-zA-Z0-9+&@#=~_|]{2,4}(:[0-9]{1,5})?(/[-a-zA-Z0-9+&@#/%?=~_|!:,;]*)*”;

private static final String PHONE_NUMBER_REGEX = “(\\d{2,}-\\d{5,})|(\\d{7,})”;

默认是系统的

3. 修改布局文件rc_item_text_message.xml,把 textview 设置成上面自定义的就可以了。

下载RongLinkify源码进行修改

RongLinkify.java.zip