TextView的onclicklistener的链接 [英] Textview onclicklistener with links

查看:118
本文介绍了TextView的onclicklistener的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML字符串,它锚一个TextView。当我点击我要拨打的TextView的,例如一个方法叫做A,当我点击TextView的链接我要调用的方法称为B.我得到这个工作,但我有一个问题:当我点击一个链接,方法B被调用,但一个方法被调用了。我怎样才能确保唯一方法B,而不是B和A,被称为当我点击一个链接?

I have a textview with a html string with anchors in it. When I click the textview I want to call eg a method called A, and when I click a link in the textview I want to call a method called B. I got this working but I got a problem: when I click a link, method B is called, but method A is called too. How can I make sure only method B, and not B and A, is called when I click a link?

我的code:

for (int i = 0; i < ingevoegd.length(); i++) {
        JSONObject soortingevoegd = ingevoegd.getJSONObject(i);
        String type = soortingevoegd.getString("type");         
        if (type.equals("Vis")) {
            String link = "<a href = 'com.aquariumzoeken.pro://Soortweergave?selected="
                    + naam + "&type=Vis" + "'>" + naam + "</a>";
            text = text.replaceAll(naam, link);
        }
    }

    TextView texttv = (TextView) v.findViewById(R.id.textviewer);   


    texttv.setText(Html.fromHtml(text));
    texttv.setMovementMethod(LinkMovementMethod.getInstance());

和TextView的onclicklistener:

And the textview onclicklistener:

texttv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {

                switchToEditMode sw = new switchToEditMode();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

在此先感谢, 西蒙

Thanks in advance, Simon

推荐答案

我做的黑客攻击你,试试这个code!

I do the hack for you, try this code!

说明:

1,采用定制ClickableSpan处理点击的URL。

1.use customized ClickableSpan to handle click on url.

2.clickablespan将TextView的前处理click事件,让一个标志一个链接被点击时。

2.clickablespan will handle the click event before the textview, make a flag when a link is clicked.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = (TextView) findViewById(R.id.main_text);
    textView.setMovementMethod(LinkMovementMethod.getInstance());

    CharSequence charSequence = textView.getText();
    SpannableStringBuilder sp = new SpannableStringBuilder(charSequence);

    URLSpan[] spans = sp.getSpans(0, charSequence.length(), URLSpan.class);

    for (URLSpan urlSpan : spans) {
        MySpan mySpan = new MySpan(urlSpan.getURL());
        sp.setSpan(mySpan, sp.getSpanStart(urlSpan),
                sp.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    }

    textView.setText(sp);

    textView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // 2.if clicking a link
            if (!isClickingLink) {
                Log.w("log", "not clicking link");
            }
            isClickingLink = false;
        }
    });
}


private boolean isClickingLink = false;

private class MySpan extends ClickableSpan {

    private String mUrl;

    public MySpan(String url) {

        super();
        mUrl = url;
    }

    @Override
    public void onClick(View widget) {

        isClickingLink = true;
        // 1. do url click
    }

}

这篇关于TextView的onclicklistener的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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