long press on selected text in textview in android [closed]

你。 提交于 2019-11-30 04:08:47

问题


In my android app, I have a text view and some text in it. I want that user can see the meaning of the words in a tool tip box when he long press on the word . My problem is that I don’t know how to implement long press in the words.Can some body help me?

Sorry for my poor English.


回答1:


use following code:

tv.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return false;
        }
});

and import:

import android.view.View.OnLongClickListener;

if you want Click just for some part Of View you need ClickableSpan. copy code from this link.

    SpannableString ss = new SpannableString("Android is a Software stack");
    //ss.setSpan(new StyleSpan(Typeface.ITALIC), 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            startActivity(new Intent(MyActivity.this, NextActivity.class));
        }
    };
    ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView textView = (TextView) findViewById(R.id.hello);
    textView.setText(ss);
    textView.setMovementMethod(LinkMovementMethod.getInstance());



回答2:


use setOnLongClickListener instead of OnClickListner



来源:https://stackoverflow.com/questions/20988905/long-press-on-selected-text-in-textview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!