如何在不同的单词上单击或点击TextView文本?

Raj*_*ddy 17 android textview spannable

如何通过单击带有两个不同单词的文本视图来移动到另一个视图.这是我正在使用的字符串.

By clicking Sign Up, you are indicating that you have read and agree to the 
Term of Use and Privacy Policy.
Run Code Online (Sandbox Code Playgroud)

我想用不同的颜色和可点击的这两个词(使用条款,隐私政策).

我知道要为特定单词制作颜色.我想让它可点击.

Boy*_*Boy 56

我终于弄明白了如何在TextView中拥有多个可点击的部分.重要的是他们都拥有自己的ClickableSpan!这是我第一次测试时出错的地方.如果它们具有相同的ClickableSpan实例,则仅记住最后设置的跨度.

我创建了一个String,其中包含所需的可点击区域,其中包含"["和"]".

String sentence = "this is [part 1] and [here another] and [another one]";
Run Code Online (Sandbox Code Playgroud)

这里是设置TextView,setMovementMehthod也是必需的:

textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(addClickablePart(sentence), BufferType.SPANNABLE);
Run Code Online (Sandbox Code Playgroud)

我创建了这个函数,它将处理可点击区域的创建:

private SpannableStringBuilder addClickablePart(String str) {
    SpannableStringBuilder ssb = new SpannableStringBuilder(str);

    int idx1 = str.indexOf("[");
    int idx2 = 0;
    while (idx1 != -1) {
        idx2 = str.indexOf("]", idx1) + 1;

        final String clickString = str.substring(idx1, idx2);
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Toast.makeText(getView().getContext(), clickString,
                        Toast.LENGTH_SHORT).show();
            }
        }, idx1, idx2, 0);
        idx1 = str.indexOf("[", idx2);
    }

    return ssb;
}
Run Code Online (Sandbox Code Playgroud)

  • 这很好,但显示的`sentence`字符串有`[`和`]`,如果它不包含在显示的字符串中? (2认同)

phy*_*lis 14

基于Boy的回答(感谢您的回复对我有很多帮助),这是我实现它的另一种方式,没有这个'['和']'字符使用内部类来描述可点击的单词:

import java.util.List;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Defines a TextView widget where user can click on different words to see different actions
 *
 */
public class ClickableTextView extends TextView {

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

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

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

    public void setTextWithClickableWords(String text, List<ClickableWord> clickableWords) {
        setMovementMethod(LinkMovementMethod.getInstance());
        setText(addClickablePart(text, clickableWords), BufferType.SPANNABLE);
    }

    private SpannableStringBuilder addClickablePart(String str, List<ClickableWord> clickableWords) {
        SpannableStringBuilder ssb = new SpannableStringBuilder(str);

        for (ClickableWord clickableWord : clickableWords) {
            int idx1 = str.indexOf(clickableWord.getWord());
            int idx2 = 0;
            while (idx1 != -1) {
                idx2 = idx1 + clickableWord.getWord().length();
                ssb.setSpan(clickableWord.getClickableSpan(), idx1, idx2, 0);
                idx1 = str.indexOf(clickableWord.getWord(), idx2);
            }
        }

        return ssb;
    }

    public static class ClickableWord {
        private String word;
        private ClickableSpan clickableSpan;

        public ClickableWord(String word, ClickableSpan clickableSpan) {
            this.word = word;
            this.clickableSpan = clickableSpan;
        }

        /**
         * @return the word
         */
        public String getWord() {
            return word;
        }

        /**
         * @return the clickableSpan
         */
        public ClickableSpan getClickableSpan() {
            return clickableSpan;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助某人

编辑:如何更改链接颜色和删除下划线:

像这样创建和使用您自己的ClickableSpan实现:

//a version of ClickableSpan without the underline
public static class NoUnderlineClickableSpan extends ClickableSpan {
    private int color = -1;

    public void setColor(int color) {
        this.color = color;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
        if (this.color != -1) {
            ds.setColor(this.color);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


paw*_*eba 7

在TextView中使用带有链接的HTML要比创建多个TextView要简单得多,要注意布局,听众等.

在您的活动或片段中:

    TextView mText = (TextView) findViewById(R.id.text_linkified);
    mText.setText(Html.fromHtml("Open <a href='myapp://my-activity'>My Activity</a> or" +
            " <a href='myapp://other-activity'>Other Activity</a>"));
    mText.setMovementMethod(LinkMovementMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)

在清单中

    <activity android:name=".MyActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="myapp" android:host="my-activity"/>
        </intent-filter>
    </activity>
    <activity android:name=".MyOtherActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="myapp" android:host="other-activity" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)