【问题标题】:android dev how to underline and change color of word in string like a link colorandroid dev如何下划线和更改字符串中单词的颜色,如链接颜色
【发布时间】:2011-02-10 15:03:22
【问题描述】:

我有一个从数据库中获取字符串并将其设置为标签的应用程序。现在我希望该标签在一个词下划线,例如“这个词应该加下划线”。我希望能够单击该下划线单词并获取其值。所以我在将它发送到数据库之前或之后设置它。谢谢你的帮助。我尝试了下面的代码,并且由于 for 循环而突出显示了每一行。请帮忙

SpannableStringBuilder builder = new SpannableStringBuilder();
            for(int i=0;i<ListClass.getLatestActivity().size();i++){
                String myString  = ListClass.getLatestActivity().get(i);
                builder.append(myString);
                String substringThatShouldBeClickable = myString.substring(0,myString.indexOf(' ')).trim();
                MySpan span = new MySpan(substringThatShouldBeClickable);
                span.setOnMySpanClickListener(mySpanOnClickListener);

                int start = 0;
                int end = builder.length();

                builder.setSpan(span, start, end, 0);
                builder.append("\n" + "\n") ;
            }
            RAInfo.setText(builder);    
            RAInfo.setMovementMethod(LinkMovementMethod.getInstance());

【问题讨论】:

  • 你能解释一下“获得它的价值”是什么意思吗?可能添加您想要的屏幕截图会有所帮助。
  • 发送到数据库之前 = static String Name = "My Name is Bob" 从数据库读取以设置标签 = "My Name Is Bob" 我希望 Bob 带有下划线和蓝色。那么这应该在数据库插入之前还是之前设置?我该怎么做?

标签: android text-formatting


【解决方案1】:

好的,所以您需要做一些事情。他们实现这一点的方法是在 TextView 中使用 span。

首先你需要一个扩展 ClickableSpan 的类:

public class MySpan extends ClickableSpan {
    public interface OnMySpanClickListener {
            public void onMySpanClick(String tag);
    }

    private final String myData;
    private OnMySpanClickListener mOnMySpanClickListener;

    public MySpan(String tag) {
            super();
            if (tag == null) {
                    throw new NullPointerException();
            }
            myData = tag;
    }

    @Override
    public void onClick(View widget) {
            if (mOnMySpanClickListener != null) {
                mOnMySpanClickListener.onMySpanClick(myData);
            }
    }

    public OnMySpanClickListener getOnMySpanClickListener() {
            return mOnMySpanClickListener;
    }

    public void setOnMySpanClickListener(OnMySpanClickListener onMySpanClickListener) {
            mOnMySpanClickListener = onMySpanClickListener;
    }
}

在您的 Activity 中,您将像这样设置 TextView 的文本:

String myString = getFromDatabase();    

SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(myString);

//You'll need to call the constructor for MySpan with only the value of the part
//of the string that you want to work with ("Bob" in the example), however you 
//determine that.
String substringThatShouldBeClickable = getMySubstring(myString);  //"Bob"
MySpan span = new MySpan(substringThatShouldBeClickable);
span.setOnMySpanClickListener(mySpanOnClickListener);

//start and end control the range of characters in the string that are clickable,
//so modify this part so it only underlines the characters you want clickable
int start = 0;
int end = bulider.length();

builder.setSpan(span, start, end, 0);

label.setText(builder);
label.setMovementMethod(LinkMovementMethod.getInstance());

最后,您需要一个处理跨度上的点击事件的处理程序:

MySpan.OnMySpanClickListener mySpanOnClickListener = new MySpan.OnMySpanClickListener() {
    public void onMySpanClick(String tag) {
        //Here is where you'll do your work with the value in the String "tag"
    }
};

希望这会有所帮助。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 2012-09-15
  • 2011-12-02
  • 1970-01-01
  • 2021-03-10
  • 2020-10-03
  • 1970-01-01
相关资源
最近更新 更多