在android上禁用setMovementMethod(LinkMovementMethod.getInstance())

dsekswqp  于 2023-05-05  发布在  Android
关注(0)|答案(7)|浏览(190)

我使用setMovementMethod(LinkMovementMethod.getInstance())在我的android应用程序中编辑文本视图。EditText内的链接工作正常,但在某些情况下,我需要编程禁用此方法(原因是启用长单击菜单中的复制项)。怎么做?我需要类似于“removeMovementMethod()"的东西。
你能帮我吗?
谢谢你!

hlswsv35

hlswsv351#

在面对同样的问题后,请考虑,似乎有一定的顺序设置的方法。
1.获取“TextView”

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

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

tv.setLayoutParams(lp_tv);

1.设置Linkify将文本中的所有链接标记为Link

tv.setAutoLinkMask(Linkify.ALL);

1.设置TextView的内容(在setAutoLinkMask之后)

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

1.现在您可以连接LinkMovementMethod。如果该方法是在前面附加的,它将调用默认行为并打开系统浏览器。我使用一个TextViewLinkHandler类来完成这项工作。标准行为是:

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

如果用户单击一个链接(例如:打开单个Intent以处理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);
    }
svujldwt

svujldwt2#

setMovementMethod(null)

会将textview的movementmethod设置为null。当然,如果你的代码想要以某种方式处理MovementMethod,这可能会导致NullPointerException。

1mrurvl1

1mrurvl13#

setMovementMethod(null)之后,longClick菜单将禁用。因此,最好使用这种方法:

setMovementMethod(ArrowKeyMovementMethod.getInstance())
smdncfj3

smdncfj34#

就像@lubart在其中一个答案中评论的那样,设置setMovementMethod(null)将不起作用。在设置LinkMovementMethod之前,需要保存以前的MovementMethod,并在不想使用LinkMovementMethod时再次恢复。
我想强调的一件重要的事情是,当你恢复到以前的MovementMethod时,你必须设置AutoLinkMask=0,否则链接不会消失,插入符号不会显示,复制/粘贴上下文菜单也不会出现。
此外,使用setMovementMethod后,在EditText/TextView中再次设置文本,以应用移动方法。
算法(不是代码):
要设置链接移动方法-

previousMovementMethod = editText.MovementMethod;

editText.autoLinkMask = All;
editText.MovementMethod = LinkMovementMethod.GetInstance();
editText.SetText(editText.GetText());

要删除链接移动方法-

editText.autoLinkMask = 0;
editText.MovementMethod = previousMovementMethod;
editText.SetText(editText.GetText());
5lhxktic

5lhxktic5#

尝试一下:-在你的项目中添加这个类

import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.ArrowKeyMovementMethod;
import android.text.method.MovementMethod;
import android.text.style.ClickableSpan;
import android.view.MotionEvent;
import android.widget.TextView;

/**
 * Created by Nishu on 20-08-2015.
 */
public class MyMovementMethod extends ArrowKeyMovementMethod {

private static MyMovementMethod sInstance;

public static MovementMethod getInstance() {
if (sInstance == null) {
    sInstance = new MyMovementMethod ();
    }
     return sInstance;
    }

@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent    event) {
int action = event.getAction();

if (action == MotionEvent.ACTION_UP ||
        action == MotionEvent.ACTION_DOWN) {
    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);

    ClickableSpan[] link = buffer.getSpans(off, off,  ClickableSpan.class);

    if (link.length != 0) {
        if (action == MotionEvent.ACTION_UP) {
            link[0].onClick(widget);
        }
        else if (action == MotionEvent.ACTION_DOWN) {
            Selection.setSelection(buffer, buffer.getSpanStart(link[0]),   buffer.getSpanEnd(link[0]));
        }

        return true;
    }
    /*else {
        that's the line we need to remove
        Selection.removeSelection(buffer);
    }*/
  }

  return super.onTouchEvent(widget, buffer, event);
 }

}

现在添加下面的代码行与您的EditText

YourEditText.setMovementMethod(MyMovementMethod.getInstance());

并从editText中删除所有类型的setMovementMethods就是这样:)

bxjv4tth

bxjv4tth6#

正如user 5211136所说,你可以使用自定义ArrowKeyMovementMethod。然后文本将是可选择的,超链接也将起作用。但是user 5211136的类的使用被称为错误
“未找到Intent的活动,Intent { act=android.intent.action.VIEW dat= https://...(has extra)}”
为了我
这就是我使用这个类的原因:

public class CustomArrowKeyMovementMethod extends ArrowKeyMovementMethod {

    public static MovementMethod getInstance(){
        return new CustomArrowKeyMovementMethod();
    }

    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event){

        int action = event.getAction();

        if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            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) {
                if (action == MotionEvent.ACTION_UP) {
                    String url = link[0].getURL().trim();
                    if(url.startsWith("www")) {
                        url = "http://" + url;
                    }
                    if (url.startsWith("https://") || url.startsWith("http://") || url.startsWith("tel:") || url.startsWith("mailto:")) {
                        try {
                            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                            widget.getContext().startActivity(browserIntent);
                        } catch (Exception e) {
                            Toast.makeText(widget.getContext(), "Couldn't open url", Toast.LENGTH_LONG).show();
                        }
                    }
                } else {
                    Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
                }
                return true;
            }
        }
        return super.onTouchEvent(widget, buffer, event);
    }
}

并称之为:

textView.setMovementMethod();
ax6ht2ek

ax6ht2ek7#

您可以使用etNote.autoLinkMask =0来禁用,使用etNote.autoLinkMask = Linkify.ALL来再次启用所有链接,只需一个按钮即可。
下面是2023年AndroidKotlin的完整代码。
在onViewCreated中提供以下内容,以初始启用所有链接。

try {
          etNote.movementMethod = LinkMovementMethod.getInstance()
          etNote.autoLinkMask = Linkify.ALL
          //etNote.setLinkTextColor(Color.parseColor("#077641"))
        } catch (e: Exception) {}

    btnHyperlinkOnOff.setOnClickListener {

        if (btnHyperlinkOnOff.text == "  Hyperlink Enable")
        {
            etNote.autoLinkMask =0
            //etNote.setLinkTextColor(Color.parseColor("#0C0C0C"))
            btnHyperlinkOnOff.text = "  Hyperlink Disable"

            //Refresh the etNote with same text to get the hyperlink disable effect...
            val contents = etNote.text.toString()
            etNoteAllowSave =false // Indirect disable afterTextChanged
            etNote.setText(contents + "\n\n\n") //set data
            etNoteAllowSave =true // Once set data done - Indirect enable afterTextChanged
        }
        else
        {
            etNote.autoLinkMask = Linkify.ALL
            //etNote.movementMethod = LinkMovementMethod.getInstance()
           // etNote.setLinkTextColor(Color.parseColor("#077641"))
            btnHyperlinkOnOff.text = "  Hyperlink Enable"

            //Refresh the etNote with same text to get the hyperlink disable effect...
            val contents = etNote.text.toString()
            etNoteAllowSave =false // Indirect disable afterTextChanged
            etNote.setText(contents + "\n\n\n") //set data
            etNoteAllowSave =true // Once set data done - Indirect enable afterTextChanged
        }
    }

相关问题