July的Android学习笔记

【Android】onLongClick for TextVie

2016-08-12  本文已影响46人  一只余

In my app I have textViews with links inside. I need to separate normal click which will open web link and longClick which will open context menu. But whenever I make longClick, when I release my finger it fires web link, which opens browser.

TextView textView = new TextView(context);
textView.setAutoLinkMask(Linkify.WEB_URLS);
textView.setText(some text with web links);
textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Log.d("RAFL", "longclick works");
return true;
}
});

textview.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        isLongClick= true;
        return true;
    }
});
textview.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP && isLongClick){
            isLongClick= false;
            return true;
        }
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            isLongClick= false;
        }
        return v.onTouchEvent(event);
    }

});
上一篇 下一篇

猜你喜欢

热点阅读