Android. Как установить событие, щелкнув фрагмент текста в TextView [duplicate]

Я нашел эту страницу после выполнения некоторых упражнений с образцами и с тем же самым загадкой.

============== Я перешел от этого кода в качестве примера ===============

public static void main(String[] args) throws IOException {

    Map wordMap = new HashMap();
    if (args.length > 0) {
        for (int i = 0; i < args.length; i++) {
            countWord(wordMap, args[i]);
        }
    } else {
        getWordFrequency(System.in, wordMap);
    }
    for (Iterator i = wordMap.entrySet().iterator(); i.hasNext();) {
        Map.Entry entry = (Map.Entry) i.next();
        System.out.println(entry.getKey() + " :\t" + entry.getValue());
    }

====================== Этот код ========================

public static void main(String[] args) throws IOException {
    // replace with TreeMap to get them sorted by name
    Map<String, Integer> wordMap = new HashMap<String, Integer>();
    if (args.length > 0) {
        for (int i = 0; i < args.length; i++) {
            countWord(wordMap, args[i]);
        }
    } else {
        getWordFrequency(System.in, wordMap);
    }
    for (Iterator<Entry<String, Integer>> i = wordMap.entrySet().iterator(); i.hasNext();) {
        Entry<String, Integer> entry =   i.next();
        System.out.println(entry.getKey() + " :\t" + entry.getValue());
    }

}

================ ================================================== =============

Это может быть безопаснее, но потребовалось 4 часа, чтобы одурачить философию ...

7
задан Rethinavel Pillai 24 February 2014 в 18:02
поделиться

2 ответа

Наконец,

я нашел решение для этого,

. Вот решение:

    SpannableString SpanString = new SpannableString(
            "By Registering you agree to the Terms of Use and Privacy Policy");

    ClickableSpan teremsAndCondition = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

            Utils.displayToast("Clickable span terms and codition",
                    SignUp.this);

            Intent mIntent = new Intent(SignUp.this, CommonWebView.class);
            mIntent.putExtra("isTermsAndCondition", true);
            startActivity(mIntent);

        }
    };

    ClickableSpan privacy = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

            Utils.displayToast("Clickable span terms and codition",
                    SignUp.this);

            Intent mIntent = new Intent(SignUp.this, CommonWebView.class);
            mIntent.putExtra("isPrivacyPolicy", true);
            startActivity(mIntent);

        }
    };

    SpanString.setSpan(teremsAndCondition, 32, 45, 0);
    SpanString.setSpan(privacy, 49, 63, 0);
    SpanString.setSpan(new ForegroundColorSpan(Color.BLUE), 32, 45, 0);
    SpanString.setSpan(new ForegroundColorSpan(Color.BLUE), 49, 63, 0);
    SpanString.setSpan(new UnderlineSpan(), 32, 45, 0);
    SpanString.setSpan(new UnderlineSpan(), 49, 63, 0);

    txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance());
    txtByRegistering.setText(SpanString, BufferType.SPANNABLE);
    txtByRegistering.setSelected(true);

благодаря Shayan pourvatan.

8
ответ дан Rethinavel Pillai 20 August 2018 в 14:50
поделиться

Предположим, что это ваша полная строка

. Подписавшись, я соглашаюсь с Условиями & amp; Политика конфиденциальности

и строка, которую вы хотите сделать кликабельными, -

Условия использования и политика конфиденциальности

, вот, вот мой трюк .....

ClickableSpan terms = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        new Utils(getActivity()).shortToast("Terms");

    }
};

ClickableSpan privacy = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        new Utils(getActivity()).shortToast("Privacy");

    }
};

основная функция для этого

public void setClickableString(String wholeValue, TextView textView, final String[] clickableValue, ClickableSpan[] clickableSpans) {
    SpannableString spannableString = new SpannableString(wholeValue);

    for (int i = 0; i < clickableValue.length; i++) {
        ClickableSpan clickableSpan = clickableSpans[i];
        String link = clickableValue[i];

        int startIndexOfLink = wholeValue.indexOf(link);
        spannableString.setSpan(clickableSpan, startIndexOfLink, startIndexOfLink + link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    textView.setHighlightColor(
            Color.TRANSPARENT); // prevent TextView change background when highlight
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(spannableString, TextView.BufferType.SPANNABLE);
}

, и вот функция, вызывающая

setClickableString(getString(R.string.terms_and_policy), tv_terms, new String[]{"Terms of Conditions", "Privacy Policy"}, new ClickableSpan[]{terms, privacy});
0
ответ дан Imran Samed 20 August 2018 в 14:50
поделиться
Другие вопросы по тегам:

Похожие вопросы: