12

次のようなテキストビューがあります。

txtByRegistering.setText("By Registering you agree to terms and condition and privacy policy");

それはただの大きなテキストです。そこで、マーキーを使用してテキストを水平方向にスクロールしました。それはうまくいきます。私の質問は、選択したスクロール テキストをクリックしているときにクリック イベントを呼び出す方法です。

例のために言う:

  1. ユーザーが上記のテキストビューで「登録中」という単語をクリックすると、新しいインテントを呼び出す必要があります。
  2. ユーザーが単語をクリックすると、"Terms"別の新しいインテント (Terms のように webview を持つアクティビティURL Link) を呼び出す必要があります。

「Registering」と「Terms」という単語は Web URL であるため、次のようにしてみました。

    String mRegDesc = "By registering you agree to the " + "<a href=\""
            + Constant.URL + "/terms_and_conditions"
            + "\">Terms of Use</a> " + "and " + "<a href=\"" + Constant.URL
            + "/privacy" + "\">Privacy Policy</a> ";

    txtByRegistering.setText(Html.fromHtml(mRegDesc));
    txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance());
    txtByRegistering.setSelected(true);
    txtByRegistering.setTypeface(mTyFaceOverLockReg, Typeface.BOLD);

上記のコードは正常に動作し、「Terms」という単語をクリックするとブラウザが表示されますが、新しいアクティビティに移動したいと考えています。

4

5 に答える 5

0

これを使用して、単一のTextViewで2回クリックしてください

Step1-: テキストは SpannableString になります

SpannableString ss = new SpannableString("By Registering you agree to terms and condition and privacy policy");

Step2:-このように ClickableSpan にクリックを追加します

 ClickableSpan Registering = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Intent intent=new Intent(this,WebView_Activity.class);
                            startActivity(intent);
        }
        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(true);
        }
    };
    ClickableSpan terms = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

            Intent intent=new Intent(this,WebView_Activity.class);
                            startActivity(intent);
        }
        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(true);
        }
    };

最後のステップでは、文字の開始インデックスと終了インデックスを使用して SpannableString をクリックし、Registering word in start at 3rd position and end at 11 のように追加します。

 ss.setSpan(Registering , 3, 11, 0);

この後の用語と同じで、TextView に SpannableString を追加します

  textview.setMovementMethod(LinkMovementMethod.getInstance());
    textview.setText(ss, TextView.BufferType.SPANNABLE);
    textview.setSelected(true);
于 2018-12-04T20:05:33.573 に答える