Android TextView,autoLink =“ all”将所有数字显示为可点击

use*_*872 1 android textview

我有一个Scrollview,并且具有属性android:clickable="true"android:autoLink="all"

我有一个用于ScrollView的字符串,并且出现了电子邮件,电话号码等并且可以正确单击。

但是,该字符串包含其他数字,例如Years,这些数字也显示为可单击,因此我不希望这样做。我该如何阻止这种情况的发生?

ata*_*ulm 5

不要用autoLink="all",用自己需要的东西

android:autoLink="web|email|phone" 可能会涵盖您的用例。

clickable="true"ScrollView不需要这个; 相反,您应该自行设置autoLink属性TextViews;如果您有其他常见属性,则可能提取样式。


将新的Linkify类添加到您的项目。从您有权访问TextView的地方(例如Activity):

TextView myTextView = // get a reference to your textview
int mask = Linkify.ALL;
Linkify.addLinks(myTextView, mask);
Run Code Online (Sandbox Code Playgroud)

addLinks(TextView, int)方法是静态的,因此无需创建的实例就可以使用它Linkify。返回值(boolean)表示是否已链接某些内容,但是您可能不需要此信息,因此我们不必理会它。

您需要确保不要将autoLink属性放在上TextViews,否则setText(...)实现仍会链接年份(除非您完全覆盖setText(...)实现而不调用super.setText(...)


对于额外的布朗尼点,您可以创建一个子类,TextView当在其上设置文本时,该子类将为您执行linkify:

public class AutoLinkifyTextView extends TextView {

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

    public AutoLinkifyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setText(String text) {
        super.setText(text);
        parseLinks();
    }

    @Override
    public void setText(int stringRes) {
        super.setText(stringRes);
        parseLinks();
    }

    private void parseLinks() {
        Linkify.addLinks(this, Linkify.ALL);
    }

}
Run Code Online (Sandbox Code Playgroud)

当然,对于最重要的方面,您应该从中读取属性,attrs并从XML属性中使用正确的掩码,但是我更希望摆脱该选项并在此处进行操作。