Android:使用 LinkMovementMethod 编辑文本

发布于 2024-12-10 01:22:06 字数 462 浏览 0 评论 0原文

我基本上想了解 LinkMovementMethod 类。我在编辑文本中有一个图像,需要可单击。所以我使用 LinkMovementMethod.getInstance 来编辑文本。但由于此 LinkMovementMethod 对象,用于编辑文本(如复制、剪切、粘贴)的上下文菜单(长按)选项不可见。我也无法将光标定位在编辑文本中。不知何故,光标隐藏了。

在编辑文本中使用 LinkMovementMethod 是正确的方法吗?还是有必要为光标和上下文菜单问题编写自定义 LinkMovementMethod 类。

     EditText editText = (EditText)findViewById(R.id.text);
     editText.setMovementMethod(LinkMovementMethod.getInstance());

-问候, 罗恩..

I basically wants to understand about LinkMovementMethod class. I am having an Image inside edit text that needs to be clickable. So I used LinkMovementMethod.getInstance for edit text. But because of this LinkMovementMethod object the context menu(long press) options for edit text like copy, cut, paste are not visible. Also i am not able to position cursor in edit text. Somehow the cursor is hiding.

Is it the correct approach to use LinkMovementMethod in edit text or is it necessary to write a custom LinkMovementMethod class for the cursor and context menu problem.

     EditText editText = (EditText)findViewById(R.id.text);
     editText.setMovementMethod(LinkMovementMethod.getInstance());

-Regards,
Ron..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许你一世情深 2024-12-17 01:22:06

您不应将 LinkMovementMethod 与可编辑文本一起使用。 LinkMovementMethod 是一种移动方法,可让您在链接之间移动...这不是您想要的文本编辑器,因为您应该使用移动光标进行编辑的移动方法。

您希望如何处理可编辑文本中的链接?当您有可编辑的文本时,点击文本会将光标置于该点进行编辑。它不会打开链接。这是两个相互矛盾的事情。如果您想要一些其他行为,您将需要自定义文本视图来执行一些特殊操作,并弄清楚如何消除编辑链接文本和打开它之间的歧义(例如,Google Docs 会向您显示一个弹出窗口)如果您不想编辑它,请选择将其打开)。但是您需要自己编写代码,在当前框架中没有神奇的内置东西可以实现这一点。

You shouldn't use LinkMovementMethod with editable text. LinkMovementMethod is a movement method that lets you move around between links... which is not what you want for a text editor, since you should be using the movement method that moves the cursor around for editing.

What do you expect to do with links in editable text? When you have editable text, tapping on the text puts the cursor at that point to edit it. It doesn't open the link. These are two conflicting things. If you want to have some other behavior, you will need to customize the text view to do something special and figure out how you are going to disambiguate between editing the link text and opening it (for example Google Docs shows you a pop-up to select to open it if you don't want to edit it). But you will need to code this yourself, there is no magic built-in thing that implements this in the current framework.

江湖正好 2024-12-17 01:22:06

一种方法是扩展EditText

import android.content.Context;
import android.text.Layout;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

public class Edit extends EditText {

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

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

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean b = super.onTouchEvent(event);

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

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

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

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

        setSelection(off);

        /**
         * get you interest span
         */
        ImageSpan[] link = getText().getSpans(off, off, ImageSpan.class);
        System.out.println("---link---" + link.length);
        if (link.length != 0) {
            String src = link[0].getSource();
            System.out.println("---src---" + src);
            return true;
        }
        return b;
    }
}

One ways is extends EditText

import android.content.Context;
import android.text.Layout;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

public class Edit extends EditText {

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

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

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean b = super.onTouchEvent(event);

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

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

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

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

        setSelection(off);

        /**
         * get you interest span
         */
        ImageSpan[] link = getText().getSpans(off, off, ImageSpan.class);
        System.out.println("---link---" + link.length);
        if (link.length != 0) {
            String src = link[0].getSource();
            System.out.println("---src---" + src);
            return true;
        }
        return b;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文