clickablespan改变颜色ACTION_DOWN(onTouch)

时间:2013-07-03 21:09:58

标签: android ontouchlistener clickable

我需要我的ClickableSpan在触摸时更改链接背景。 那是我的代码:

holder.text.setMovementMethod(LinkMovementMethod.getInstance());
    holder.text.setText(addClickablePart(post.getTweet().getText()), BufferType.SPANNABLE);

    private SpannableStringBuilder addClickablePart(String str) {
SpannableStringBuilder ssb = new SpannableStringBuilder(str);

int idx1 = str.indexOf("#");
int idx2 = 0;
while (idx1 != -1) {
    idx2 = str.indexOf(" ", idx1);
    if(idx2 == -1) {
        idx2 = str.length();
    }

    final String clickString = str.substring(idx1, idx2);
    ssb.setSpan(new ClickableSpan() {
        @Override
                public void updateDrawState(TextPaint ds) {
            ds.setUnderlineText(false);
                    super.updateDrawState(ds);
                }

                @Override
        public void onClick(View widget) {
            if(clickString.startsWith("#")) {
                Toast.makeText(getView().getContext(), "left",
                Toast.LENGTH_SHORT).show();
            } 
        }
    }, idx1, idx2, 0);
    idx1 = str.indexOf("#", idx2);
}

我看不出我怎么能把它拉下来...... 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用像这样的后台xml: 放入你的drawable文件夹 并在你的旋转器中设置:

android:background="@drawable/YOUR_XML_FILE"

xml文件

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape

     ///WHEN PRESSED TO CODE HERE WILL BE ACTIVATED///

    </shape>
  </item>
<item>
    <shape

     ////YOU CODE FOR WHEN NOT PRESSED///

    </shape>
  </item>
</selector>