本文介绍了里面的TextView点击单词机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的TextView与动态变化的文本。本文包含类似&LT字符串; A HREF =myWord'&GT​​; myWord< / A> 。我想要点击这个链接后myWord出现在EditText上的同一个活动。

这是我的code:

  txt.setText(Html.fromHtml(...< A HREF ='链接'>链接< / A> ...));
txt.setMovementMethod(LinkMovementMethod.getInstance());
 

这对内部href属性的URL工作得很好,但有一个错误的另一种格式。

我发现了很多关于StackOverflow的类似的问题,但他们都是关于URL链接。在我的应用我想里面创建活动链接。在一般情况下,我可以改变标签的一些另外,如果它取决于...

请帮帮我!谢谢!

-----解决-----感谢您雅各布菲利普斯的想法!

也许这将是有趣的人的未来。这是一个code:

  //这是我的字符串;
字符串str =&其中b取代;文字< / B个,其中包含一个实施; A HREF =#>链路其中; /一>和另一与所述; A HREF =#>链路其中; / A>中;
// TextView的;
TextView的TXT =新的TextView(本);
//分割字符串部分:
串[] devFull =数据[v.getId()] [1] .split(&所述; A HREF =#>中);
//添加第一部分:
txt.append(Html.fromHtml(devFull [0]));
//创建数组部分的链接(它们等同始终将devFull.length-1):
SpannableString []链接=新SpannableString [devFull.length-1];
//本地变量:
ClickableSpan [] CS =新ClickableSpan [devFull.length-1];
字符串linkWord;
的String [] devDevFull =新的String [2];

的for(int i = 1; I< devFull.length;我++){
    //获得清除链接
    devDevFull = devFull [Ⅰ] .split(&所述; / A>中);
    链路[I-1] =新SpannableString(devDevFull [0]);
    linkWord = devDevFull [0];
    CS [I-1] =新ClickableSpan(){
        私人字符串W = linkWord;
        @覆盖
        公共无效的onClick(View部件){
            //这里你可以使用W(linkWord)
        }
    };
    链路[I-1] .setSpan(CS [Ⅰ-1],0,linkWord.length(),0);
    txt.append(联系[I-1]);
    尝试{
        txt.append(Html.fromHtml(devDevFull [1]));
    }
    赶上(例外五){}
}
 

解决方案

这应该做的伎俩。只需更改 OnClickListener 您的EditText文本。它可以是能够减小但这应该工作

 私人无效美孚(){
    SpannableString链接= makeLinkSpan(点击这里,新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            //响应点击
        }
    });

    //我们需要一个TextView实例。
    TextView的电视=新的TextView(上下文);

    //设置TextView的文本
    tv.setText(执行行动);

    //将上面我们使用下面定义的函数创建的链接。
    tv.append(联系);

    //附加一个周期(这不会是一个链接)。
    tv.append(。);

    //这条线使链接点击!
    makeLinksFocusable(电视);
}

/ *
 *以上使用的方法。
 * /

私人SpannableString makeLinkSpan(CharSequence的文字,View.OnClickListener监听器){
    SpannableString链接=新SpannableString(文本);
    link.setSpan(新ClickableString(侦听),0,text.length(),
        SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
    返回的链接;
}

私人无效makeLinksFocusable(TextView的电视){
    MovementMethod米= tv.getMovementMethod();
    如果((M == NULL)||!(M的instanceof LinkMovementMethod)){
        如果(tv.getLinksClickable()){
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }
}

/ *
 * ClickableString类
 * /

私有静态类ClickableString扩展ClickableSpan {
    私人View.OnClickListener mListener;
    公共ClickableString(View.OnClickListener监听器){
        mListener =侦听器;
    }
    @覆盖
    公共无效的onClick(视图v){
        mListener.onClick(五);
    }
}
 

I have TextView with text that changed dynamically. This text contain strings like <a href='myWord'>myWord</a>. I want that after click to this "link" myWord appear in the EditText in the same activity.

This is my code:

txt.setText(Html.fromHtml("...<a href='link'>link</a>..."));
txt.setMovementMethod(LinkMovementMethod.getInstance());

It's work well for URLs inside href attribute, but there is an error for another format.

I found a lot of similar questions on the StackOverflow but all of them were about url links. In my app I want create "link" inside activity.In general, I can change tag to some another if it's depend...

Please help me!Thank you!

-----SOLVED-----Thank you Jacob Phillips for idea!

May it will be interesting someone in future.This is a code:

//This is my string;
String str = "<b>Text</b> which contains one <a href='#'>link</a> and another <a href='#'>link</a>";
//TextView;
TextView txt = new TextView(this);
//Split string to parts:                                        
String[] devFull = data[v.getId()][1].split("<a href='#'>");
//Adding first part:
txt.append(Html.fromHtml(devFull[0]));
//Creating array for parts with links (they amount always will devFull.length-1):
SpannableString[] link = new SpannableString[devFull.length-1];
//local vars:
ClickableSpan[] cs = new ClickableSpan[devFull.length-1];
String linkWord;
String[] devDevFull = new String[2];

for(int i=1; i<devFull.length; i++){
    //obtaining 'clear' link
    devDevFull = devFull[i].split("</a>");
    link[i-1] = new SpannableString(devDevFull[0]);
    linkWord = devDevFull[0];
    cs[i-1] = new ClickableSpan(){
        private String w = linkWord;
        @Override
        public void onClick(View widget) {
            // here you can use w (linkWord)
        }
    };
    link[i-1].setSpan(cs[i-1], 0, linkWord.length(), 0);
    txt.append(link[i-1]);
    try{
        txt.append(Html.fromHtml(devDevFull[1]));
    }
    catch(Exception e){}
}
解决方案

This should do the trick. Just change your edittext's text in the OnClickListener. It may be able to be reduced but this should work.

private void foo() {
    SpannableString link = makeLinkSpan("click here", new View.OnClickListener() {          
        @Override
        public void onClick(View v) {
            // respond to click
        }
    });

    // We need a TextView instance.        
    TextView tv = new TextView(context);   

    // Set the TextView's text     
    tv.setText("To perform action, ");

    // Append the link we created above using a function defined below.
    tv.append(link);

    // Append a period (this will not be a link).
    tv.append(".");

    // This line makes the link clickable!
    makeLinksFocusable(tv);
}

/*
 * Methods used above.
 */

private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener) {
    SpannableString link = new SpannableString(text);
    link.setSpan(new ClickableString(listener), 0, text.length(), 
        SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
    return link;
}

private void makeLinksFocusable(TextView tv) {
    MovementMethod m = tv.getMovementMethod();  
    if ((m == null) || !(m instanceof LinkMovementMethod)) {  
        if (tv.getLinksClickable()) {  
            tv.setMovementMethod(LinkMovementMethod.getInstance());  
        }  
    }  
}

/*
 * ClickableString class 
 */

private static class ClickableString extends ClickableSpan {  
    private View.OnClickListener mListener;          
    public ClickableString(View.OnClickListener listener) {              
        mListener = listener;  
    }          
    @Override  
    public void onClick(View v) {  
        mListener.onClick(v);  
    }        
}

这篇关于里面的TextView点击单词机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 02:01