在Android上禁用setMovementMethod(LinkMovementMethod.getInstance()) [英] disable setMovementMethod(LinkMovementMethod.getInstance()) on android

查看:313
本文介绍了在Android上禁用setMovementMethod(LinkMovementMethod.getInstance())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用程序中将setMovementMethod(LinkMovementMethod.getInstance())用于EditText视图. EditText内的链接工作正常,但在某些情况下,我需要以编程方式禁用此方法(出于在longClick菜单中启用复制项的原因).怎么做?我需要"removeMovementMethod()"之类的东西.

I'm using setMovementMethod(LinkMovementMethod.getInstance()) for EditText view in my android app. Links inside EditText works fine but in some situations I need programly disable this method (for reason of enable copy item in longClick menu). How to do it? I need something like "removeMovementMethod()".

你能帮我吗?

谢谢!

推荐答案

在遇到同样的问题后,请考虑一下,似乎有一定的设置方法的顺序.

After facing the same issue as well, please consider, there seems to be a certain sequence of setting the methods.

  1. 获取"TextView"

final TextView tv = new TextView(getContext());

  1. 设置所有必要的布局参数

tv.setLayoutParams(lp_tv);

  1. 设置Linkify以将文本中的所有链接标记为Link
  1. Set the Linkify to mark all links in the text as Link

tv.setAutoLinkMask(Linkify.ALL);

  1. 设置TextView的内容(在setAutoLinkMask之后)
  1. Set the content of the TextView (after the setAutoLinkMask)

tv.setText("MyText")
tv.setText(Html.fromHtml("<big>MyText</big>");

  1. 现在您可以附加LinkMovementMethod.如果该方法是较早附加的,它将调用默认行为并打开系统浏览器.我使用TextViewLinkHandler类作为个人行为来完成这项工作.标准行为是:
  1. Now you can attach the LinkMovementMethod. If the method is attached earlier, it will call the default behavior and open the system browser. I use a TextViewLinkHandler class to do the job as individual behavior. The standard behavior is:

tv.setLinkMovementMethod(new LinkMovementMethod.getInstance());

如果用户点击链接(例如,打开一个单独的Intent处理网址),我会使用TextViewLinkHandler做其他事情

I use the TextViewLinkHandler to do some other stuff if the user clicks a link (e.g. opening an individual Intent to process the URL)

 tv.setMovementMethod(new TextViewLinkHandler() {
// do my stuff ... 
// if left blank, nothing will happen on click at the link, so leave it blank to do nothing
});

使用提到的TextViewLinkHandler()

public abstract class TextViewLinkHandler extends LinkMovementMethod {

        public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
            if (event.getAction() != MotionEvent.ACTION_UP)
                return super.onTouchEvent(widget, buffer, event);

            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
            if (link.length != 0) {
                onLinkClick(link[0].getURL());
            }
            return true;
        }

        abstract public void onLinkClick(String url);
    }

这篇关于在Android上禁用setMovementMethod(LinkMovementMethod.getInstance())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆