2 zhenglingkun zhenglingkun 于 2013.05.15 15:57 提问

ListView的item中实现@功能,点击时跳转执行两次的问题,

下面是一个自定义的TextView:
`public class TextViewFixTouchConsume extends TextView {

public TextViewFixTouchConsume(Context context) {
    super(context);
}

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

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

@Override
public boolean onTouchEvent(MotionEvent event) {

  if (getMovementMethod() == null ) {
        boolean result = super.onTouchEvent(event); 
        return result;
    }

    MovementMethod m = getMovementMethod();     
    setMovementMethod(null);

    boolean mt = m.onTouchEvent(this, (Spannable) getText(), event);
    if (mt && event.getAction() == MotionEvent.ACTION_DOWN) {
        event.setAction(MotionEvent.ACTION_UP);
        mt = m.onTouchEvent(this, (Spannable) getText(), event);
        event.setAction(MotionEvent.ACTION_DOWN);
    }

    boolean st = super.onTouchEvent(event);

    setMovementMethod(m);
    setFocusable(false);

    return mt || st;
}

}`
下面是在adapter中写的代码:

mHolder.content.setText(parser.replace(string, true));
        mHolder.content.setMovementMethod(LinkMovementMethod.getInstance());

下面是replace方法以及自定义的ClickableSpan:

public CharSequence replace(CharSequence text, boolean flag) {
    text = Html.fromHtml(MicroblogUtil.getLinkString(text.toString()));
    SpannableStringBuilder builder = new SpannableStringBuilder(text);
    if (flag) {
        if (text instanceof Spannable) {
            int end = text.length();
            // 其实就是得到sp1
            Spannable sp = (Spannable) text;
            // 得到其中所有的关键字的数组
            URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
            // 清楚掉所有的关键字标志
            builder.clearSpans();
            for (URLSpan url : urls) {
                // 将关键字数组中的文字添加到新生成的style中去
                MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
                builder.setSpan(myURLSpan, sp.getSpanStart(url),
                        sp.getSpanEnd(url),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    } else {
        Matcher matcher = mPattern.matcher(text);
        while (matcher.find()) {
            int resId = mSmileyToRes.get(matcher.group());
            builder.setSpan(new ImageSpan(mContext, resId),
                    matcher.start(), matcher.end(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    return builder;
}

/**
 * 继承于可点击的标签
 * 
 * @author excuce
 * 
 */
private class MyURLSpan extends ClickableSpan {
    private String mUrl;

    /**
     * 构造器
     * 
     * @param url
     *            可以点击的关键字,构造时传入的
     */
    public MyURLSpan(String url) {
        mUrl = url;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setColor(ds.linkColor);
        ds.setUnderlineText(false);// <span
                                    // style="color: red;">//去掉下划线</span>
    }

    @Override
    public void onClick(View widget) {
        // System.out.println(mUrl.trim().substring(ONE));
        Intent intent = new Intent(mContext, WeiBoPersonalActivity.class);
        intent.putExtra(KEY_NAME, mUrl.trim().substring(ONE));
        mContext.startActivity(intent);
        // Log.d("MyURLSpan", "MyURLSpan");
    }
}

帮忙看看是怎么回事?这个方法我在模拟器上面试过没有出现跳转两次的问题,但在真机上出现了。

1个回答

zhenglingkun
zhenglingkun   2013.05.15 17:15
已采纳

@Override
public boolean onTouchEvent(MotionEvent event) {

    int action = event.getAction();
    boolean flag = false;

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        MovementMethod m = getMovementMethod();
        setMovementMethod(null);
        boolean mt = m.onTouchEvent(this, (Spannable) getText(), event);
        if (mt) {
            event.setAction(MotionEvent.ACTION_UP);
            mt = m.onTouchEvent(this, (Spannable) getText(), event);
            event.setAction(MotionEvent.ACTION_DOWN);
        }
        boolean st = super.onTouchEvent(event);
        setMovementMethod(m);
        setFocusable(false);
        flag = mt || st;
        break;
    default:
        break;
    }
    return flag;
}

改一下这个方法就行!

a327037419
a327037419 有没有什么解决方法
一年多之前 回复
a327037419
a327037419 我遇到的换行的数据,TextViewFixTouchConsume直接处理成一行了
一年多之前 回复
zw8362
zw8362 点击改变背景色怎么弄?
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!