如何显示在textViews联系,并能够正确地处理它们呢? [英] How to show links on textViews and be able to handle them correctly?

查看:145
本文介绍了如何显示在textViews联系,并能够正确地处理它们呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

您说这是一个重新发布之前,我必须说,我曾尝试阅读其他职位,所以请继续阅读。

显示一个TextView内部链接的基本方法可以在接下来的方式来完成:

 最后跨区文本= Html.fromHtml(的getString(R.string.test));
textView.setText(文本);
textView.setMovementMethod(LinkMovementMethod.getInstance());

和string.xml文件可能包含:

 <字符串名称=测试格式=false的>
<![CDATA [
        该< BR />
        为< BR />
        &所述;&下; A HREF =htt​​p://www.google.com>一种与所述; / A>&所述峰; br />
        测试
        ]]>
    < /串>

但是,我们可能要捕捉点击链接的事件,处理它我们自己。不仅如此,但链接可能不需要成为一个真正的url。

很多帖子的存在是为了展示如何做到这一点,但没有,我试过应付自如。

有些让正在意向上的应用程序,这是相当繁琐,可能意味着其他应用程序可以处理太(右?),我认为它需要的链接将是一个特定的模式来处理。

一些让LinkMovementMethod处理,但破坏链接的可点击的效果。有人说改变颜色的东西,但是这是不一样的。

问题

你怎么一个简单的监听器添加到TextView的,将告诉你哪些文本区域被点击里面?

我已经试过

目前,我用LinkMovementMethod。我的code的问题是:


  • 因为我不知道该怎么模仿点击作为正常的链接,当你点击它看起来怪怪的。背景看起来不正确,我想我需要使用默认设备之一,但不能找出如何。作为oppsosed什么有些人可能会说,这是无法通过下一个code:


  

textview.getLinkTextColors()。getDefaultColor()



    在某些情况下,文本可能保持点击

这里的code:

MainActivity.java(如何使用示例)

 公共类MainActivity延伸活动{    @覆盖
    公共无效的onCreate(最终捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        最后字符串一个=&下; A HREF ='/ A'GT; AAAA&下; / A> 123456&下; A HREF ='/ B'> BBBB&下; / A> 7890;
        最终的TextView的TextView =(的TextView)findViewById(R.id.span);
        textView.setText(Html.fromHtml(一));
        最后LinkClickMovementMethodExt linkMovementMethod =新LinkClickMovementMethodExt();
        linkMovementMethod.setOnLinkClickedListener(新OnLinkClickedListener(){            @覆盖
            公共无效onLinkClicked(最终的TextView clickedTextView,最终URLSpan clickedSpan){
                Toast.makeText(MainActivity.this,clickedSpan.getURL(),Toast.LENGTH_SHORT).show();
            }
        });
        textView.setMovementMethod(linkMovementMethod);
    }}

LinkClickMovementMethodExt.java

  / **一个类来处理点击textViews的链接。基于http://stackoverflow.com/a/16182500/878126 * /
公共类LinkClickMovementMethodExt扩展LinkMovementMethod {
    // TODO检查如何获取点击链接的默认背景
    私人最终BackgroundColorSpan LINK_COLOR =新BackgroundColorSpan(0xFFACE0F4);
    私人最终级< URLSpan> spanClass = URLSpan.class;
    私人OnLinkClickedListener mOnLinkClickedListener;    公共接口OnLinkClickedListener {
        公共无效onLinkClicked(TextView的TextView的,URLSpan clickedSpan);
    }    @覆盖
    公共布尔onTouchEvent(最终的TextView的TextView,最终Spannable缓冲,最终MotionEvent事件){
        最终诠释行动= event.getAction();        如果(动作== || MotionEvent.ACTION_UP行动== MotionEvent.ACTION_DOWN){
            INT X =(int)的event.getX();
            INT Y =(int)的event.getY();            点¯x - = textView.getTotalPaddingLeft();
            Ÿ - = textView.getTotalPaddingTop();            X + = textView.getScrollX();
            Y + = textView.getScrollY();            最终的Layout布局= textView.getLayout();
            最终诠释行= layout.getLineForVertical(Y);
            最终诠释了= layout.getOffsetForHorizo​​ntal(线,X);
            / **
             *让你感兴趣的跨度
             * /
            最终目标[] =跨度buffer.getSpans(脱,脱,spanClass);
            如果(spans.length!= 0){
                如果(动作== MotionEvent.ACTION_DOWN){
                    Selection.setSelection(缓冲液,buffer.getSpanStart(跨距[0]),buffer.getSpanEnd(跨距[0]));
                    对于(最终目标跨度:跨度){
                        如果(跨度的instanceof URLSpan){
                            最终诠释启动= Selection.getSelectionStart(textView.getText());
                            最终诠释结束= Selection.getSelectionEnd(textView.getText());
                            最终Spannable selectedSpan =(Spannable)textView.getText();
                            selectedSpan.setSpan(LINK_COLOR,开始,结束,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            textView.setText(selectedSpan);
                            如果(mOnLinkClickedListener!= NULL)
                                mOnLinkClickedListener.onLinkClicked(TextView的,(URLSpan)跨度);
                        }
                    }
                    返回false;
                }否则如果(动作== MotionEvent.ACTION_UP){
                    最后Spannable跨度=(Spannable)textView.getText();
                    span.removeSpan(LINK_COLOR);
                    textView.setText(跨度);
                    返回false;
                }
            }
        }
        返回super.onTouchEvent(TextView的,缓冲液,事件);
    }    @覆盖
    公共布尔canSelectArbitrarily(){
        返回true;
    }    @覆盖
    公共布尔的onkeyup(最终的TextView小部件,最终Spannable缓冲,最终诠释键code,最终KeyEvent的事件){
        返回false;
    }    公共无效setOnLinkClickedListener(最终OnLinkClickedListener onLinkClickedListener){
        this.mOnLinkClickedListener = onLinkClickedListener;
    }
}


解决方案

在XML:

 <的TextView
    机器人:ID =@ + ID / MSGTEXT
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =10dip
    机器人:linksClickable =真
    机器人:文字颜色=#000000
    机器人:textColorLink =#EE4E1D
    机器人:文字=TextView的/>

在Java:

  TextView的tvmsg =(TextView中)textEntryView.findViewById(R.id.msgtext);
         tvmsg.setMovementMethod(LinkMovementMethod.getInstance());
         tvmsg.setText(Html.fromHtml(消息));

Background

before you say it's a repost, i have to say i have tried reading other posts, so please keep reading.

The basic way to show links inside a textView can be done in the next way:

final Spanned text = Html.fromHtml(getString(R.string.test));
textView.setText(text);
textView.setMovementMethod(LinkMovementMethod.getInstance());

and the string.xml file could contain :

<string name="test" formatted="false">
<![CDATA[
        This<br />
        is<br />
        <<a href="http://www.google.com">a</a><br />
        test
        ]]>
    </string>

However, we might want to capture the event of clicking on the link, and handle it ourself. not only that, but the link might not need to be of a real url.

many posts exists to show how to do it, but none that i've tried handle it well.

some let the intent being handled on the app, which is quite cumbersome and might mean other apps could handle it too (right?) and i think it requires that the link would be to a specific pattern.

some let the LinkMovementMethod handle it but ruin the clickability effect of the links. some say to change the color to something, but that's not the same.

The problem

How do you simply add a listener to the textview that will tell you which text area was clicked inside it?

What I've tried

currently, i've used LinkMovementMethod . the issues of my code are:

  • because i don't know how to mimic the clicking as on normal links, it looks weird when you click on it. the background doesn't look right, and i think i need to use the default one of the device, but can't find out how. as oppsosed to what some may say, it's not by using the next code:

textview.getLinkTextColors().getDefaultColor()

  • on some cases the text might stay clicked .

here's the code:

MainActivity.java (sample of how to use)

public class MainActivity extends Activity {

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String a = "<a href='/a'>aaaa</a>123456<a href='/b'>bbbb</a>7890";
        final TextView textView = (TextView) findViewById(R.id.span);
        textView.setText(Html.fromHtml(a));
        final LinkClickMovementMethodExt linkMovementMethod = new LinkClickMovementMethodExt();
        linkMovementMethod.setOnLinkClickedListener(new OnLinkClickedListener() {

            @Override
            public void onLinkClicked(final TextView clickedTextView, final URLSpan clickedSpan) {
                Toast.makeText(MainActivity.this, clickedSpan.getURL(), Toast.LENGTH_SHORT).show();
            }
        });
        textView.setMovementMethod(linkMovementMethod);
    }

}

LinkClickMovementMethodExt.java

/** a class to handle clicking on links of textViews . based on http://stackoverflow.com/a/16182500/878126 */
public class LinkClickMovementMethodExt extends LinkMovementMethod {
    // TODO check how to get the default background of a clicked link
    private final BackgroundColorSpan LINK_COLOR = new BackgroundColorSpan(0xFFACE0F4);
    private final Class<URLSpan> spanClass = URLSpan.class;
    private OnLinkClickedListener mOnLinkClickedListener;

    public interface OnLinkClickedListener {
        public void onLinkClicked(TextView textView, URLSpan clickedSpan);
    }

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

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

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

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

            final Layout layout = textView.getLayout();
            final int line = layout.getLineForVertical(y);
            final int off = layout.getOffsetForHorizontal(line, x);
            /**
             * get you interest span
             */
            final Object[] spans = buffer.getSpans(off, off, spanClass);
            if (spans.length != 0) {
                if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(buffer, buffer.getSpanStart(spans[0]), buffer.getSpanEnd(spans[0]));
                    for (final Object span : spans) {
                        if (span instanceof URLSpan) {
                            final int start = Selection.getSelectionStart(textView.getText());
                            final int end = Selection.getSelectionEnd(textView.getText());
                            final Spannable selectedSpan = (Spannable) textView.getText();
                            selectedSpan.setSpan(LINK_COLOR, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            textView.setText(selectedSpan);
                            if (mOnLinkClickedListener != null)
                                mOnLinkClickedListener.onLinkClicked(textView, (URLSpan) span);
                        }
                    }
                    return false;
                } else if (action == MotionEvent.ACTION_UP) {
                    final Spannable span = (Spannable) textView.getText();
                    span.removeSpan(LINK_COLOR);
                    textView.setText(span);
                    return false;
                }
            }
        }
        return super.onTouchEvent(textView, buffer, event);
    }

    @Override
    public boolean canSelectArbitrarily() {
        return true;
    }

    @Override
    public boolean onKeyUp(final TextView widget, final Spannable buffer, final int keyCode, final KeyEvent event) {
        return false;
    }

    public void setOnLinkClickedListener(final OnLinkClickedListener onLinkClickedListener) {
        this.mOnLinkClickedListener = onLinkClickedListener;
    }
}

解决方案

In XML:

<TextView
    android:id="@+id/msgtext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dip"
    android:linksClickable="true"
    android:textColor="#000000"
    android:textColorLink="#EE4E1D"
    android:text="TextView" />

In Java:

TextView tvmsg = (TextView)textEntryView.findViewById(R.id.msgtext);
         tvmsg.setMovementMethod(LinkMovementMethod.getInstance());
         tvmsg.setText(Html.fromHtml(message));

这篇关于如何显示在textViews联系,并能够正确地处理它们呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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