19

2 つの異なる単語を含むテキスト ビューをクリックして、別のビューに移動する方法。これは私が使用している文字列です。

By clicking Sign Up, you are indicating that you have read and agree to the 
Term of Use and Privacy Policy.

この 2 つの単語 (利用規約、プライバシー ポリシー) を別の色にして、クリックできるようにしたい..

特定の単語に色を付ける方法を知っています。クリック可能にしたい。

4

5 に答える 5

60

TextView に複数のクリック可能なパーツを配置する方法がようやくわかりました。それらすべてに独自の ClickableSpan があることが重要です。それは私が最初にそれをテストしたときに間違ったところです。それらが同じ ClickableSpan インスタンスを持っている場合、最後に設定されたスパンのみが記憶されます。

「[」と「]」で囲まれた必要なクリック可能な領域を持つ文字列を作成しました。

String sentence = "this is [part 1] and [here another] and [another one]";

ここにセット TextView があります。setMovementMehtod も必須です。

textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(addClickablePart(sentence), BufferType.SPANNABLE);

クリック可能な領域の作成を処理するこの関数を作成しました。

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;
}
于 2012-07-11T18:10:52.057 に答える
15

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;
        }
    }
}

これが誰かを助けることを願っています

編集:リンクの色を変更して下線を削除する方法:

次のような 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);
        }
    }
}
于 2014-01-24T01:10:28.423 に答える
2

このライブラリをお勧めします: https://github.com/klinker24/Android-TextView-LinkBuilder

それはあなたの要件に非常によく合います。

簡単な概要:

ここに画像の説明を入力

プロジェクトの readme からコピー:

  • TextView 内の特定の単語の長いクリック アクションと短いクリック アクションを指定する
  • ユーザーがテキストに触れたときにテキストを強調表示して、ユーザー フィードバックを提供する 単一の文字列に一致させるか、正規表現を使用して、そのパターンに準拠する任意のテキストへのクリック可能なリンクを設定する
  • リンクされたテキストの色を変更する
  • ユーザーがテキストに触れたときのテキストの強調表示の透明度を変更します
  • テキストに下線を付けるかどうかを設定します

TextView の自動リンク機能よりもこのライブラリを使用する主な利点は、Web アドレス、電子メール、電話番号だけでなく、何でもリンクできることです。また、色のカスタマイズとタッチ フィードバックも提供します。

于 2015-07-03T08:36:50.790 に答える