【问题标题】:How to set part of TextView clickable while hiding the link?如何在隐藏链接时设置部分 TextView 可点击?
【发布时间】:2018-03-23 19:05:52
【问题描述】:

我有一个 TextView 来显示电子邮件的收件人,原始字符串将类似于 "FirstName LastName <xx@yy.com>, FirstName LastName <xx@yy.com>, FirstName LastName <xx@yy.com>"。我想显示像"FirstName LastName, FirstName LastName, FirstName LastName" 这样的字符串,而FirstName LastName 是可点击的。我知道如何获取FirstName LastName,并且我知道如何使用URLSpan 使xx@yy.com 可点击,但它只会使xx@yy.com 可点击,我想要做的是只显示FirstName LastName 并使其可点击, 任何想法?

【问题讨论】:

标签: java android textview spannable


【解决方案1】:

我通过扩展 ClickableSpan 并传入隐藏信息找到了解决方案,如下面的代码。

public class RecipientsSpan extends ClickableSpan {
    public String name;
    private String emailAddress;
    private int textColor;

    private RecipientsClickHandler recipientsClickHandler;

    public RecipientsSpan(String name, String emailAddress, int textColor) {
        super();
        this.name = name;
        this.emailAddress = emailAddress;
        this.textColor = textColor;
    }

    /**
     * Set the interface that will be used to handle the click event.
     * @param recipientsClickHandler interface to pass in.
     */
    public void setRecipientsClickHandler(@NonNull RecipientsClickHandler recipientsClickHandler) {
        this.recipientsClickHandler = recipientsClickHandler;
    }

    /**
     * Interface to handle click event for different spans inside the same TextView.
     */
    public interface RecipientsClickHandler {
        void onRecipientsClicked(@NonNull String name, @NonNull String emailAddress);
    }

    @Override
    public void onClick(View view) {
        if (recipientsClickHandler != null) {
            recipientsClickHandler.onRecipientsClicked(name, emailAddress);
        }
    }

    /**
     * Make text show the color specified and also avoid the default underline.
     * @param ds TextPaint that will be applied to the text.
     */
    @Override
    public void updateDrawState(@NonNull TextPaint ds) {
        ds.setColor(textColor);
        ds.setUnderlineText(false);
    }
}

【讨论】:

    【解决方案2】:

    对于每个 TextView,您可以在 Java 代码中设置一个 onClick 侦听器。 因此,您可以轻松地将 TextView 与 mailadress 链接

    【讨论】:

    • 只有一个文本视图,而不是多个。
    • 然后像这里建议的那样做stackoverflow.com/q/10696986/4417786
    • 最大的问题是我需要隐藏一些文本,同时让其他文本可点击。
    • 有足够的方法和方法来改变字符串!所以你不隐藏文本,你删除它
    • 我怎么知道我当时点击了哪封电子邮件?你有什么可以参考的吗?谢谢!
    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多