Android上TextView中的多个可点击链接

时间:2015-02-25 13:22:12

标签: java android android-layout textview

我试图在文本视图中添加多个链接,类似于Google& Flipboard在下面的屏幕截图中显示了条款和条件 AND 隐私政策

到目前为止,我偶然发现了使用这种方法

textView.setText(Html.fromHtml(MYHTML);  textView.setMovementMethod(LinkMovementMethod.getInstance());

其中myHtml是href。

但它并没有给我控制,例如我需要启动片段等。

在下面的两个例子中,他们是如何实现这一点的?

Flipboard terms and conditions view

Google terms and conditions view

7 个答案:

答案 0 :(得分:73)

我认为我分享这个有点晚了,但我使用SpannableStringBuilder取得了相同的成绩。

只需初始化要添加2个或更多侦听器的TextView,然后将其传递给我创建的以下方法:

private void customTextView(TextView view) {
        SpannableStringBuilder spanTxt = new SpannableStringBuilder(
                "I agree to the ");
        spanTxt.append("Term of services");
        spanTxt.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(getApplicationContext(), "Terms of services Clicked",
                        Toast.LENGTH_SHORT).show();
            }
        }, spanTxt.length() - "Term of services".length(), spanTxt.length(), 0);
        spanTxt.append(" and");
        spanTxt.setSpan(new ForegroundColorSpan(Color.BLACK), 32, spanTxt.length(), 0);
        spanTxt.append(" Privacy Policy");
        spanTxt.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(getApplicationContext(), "Privacy Policy Clicked",
                        Toast.LENGTH_SHORT).show();
            }
        }, spanTxt.length() - " Privacy Policy".length(), spanTxt.length(), 0);
        view.setMovementMethod(LinkMovementMethod.getInstance());
        view.setText(spanTxt, BufferType.SPANNABLE);
    } 

在您的XML中,使用android:textColorLink添加您选择的自定义链接颜色。像这样:

   <TextView
     android:id="@+id/textView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="TextView"
     android:textColorLink="#C36241" />  //#C36241 - Rust

这看起来像这样:

enter image description here

希望它有所帮助。 :)

答案 1 :(得分:12)

您可以使用Linkify android.text.Spannable java.util.regex.Pattern java.lang.String

String termsAndConditions = getResources().getString(R.string.terms_and_conditions);
String privacyPolicy = getResources().getString(R.string.privacy_policy);

legalDescription.setText(
    String.format(
        getResources().getString(R.string.message),
        termsAndConditions,
        privacyPolicy)
);
legalDescription.setMovementMethod(LinkMovementMethod.getInstance());

Pattern termsAndConditionsMatcher = Pattern.compile(termsAndConditions);
Linkify.addLinks(legalDescription, termsAndConditionsMatcher, "terms:");

Pattern privacyPolicyMatcher = Pattern.compile(privacyPolicy);
Linkify.addLinks(legalDescription, privacyPolicyMatcher, "privacy:");

然后您可以使用该方案启动活动,例如在 AndroidManifest 中添加方案:

<intent-filter>
    <category android:name="android.intent.category.DEFAULT" />
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="terms" />
    <data android:scheme="privacy" />
</intent-filter>

如果要执行自定义操作,可以将intent-filter设置为当前活动,该活动将具有singleTop启动模式。

这会导致onNewIntent被解雇,可以进行自定义操作:

@Override
protected void onNewIntent(final Intent intent) {
 ...
  if (intent.getScheme().equals(..)) {
    ..
  }
}

答案 2 :(得分:2)

这是我的解决方案:

首先,我们需要在T​​extView中添加可点击链接:

  1. 这里是xml布局中的TextView,不添加任何链接处理 参数。

    <TextView
            android:id="@+id/sign_up_privacy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/terms_and_privacy"/>
    
  2. 在字符串文件中,我添加了带有html标记的资源文本

    <string name="terms_and_privacy">By signing up you agree to our <a href="terms:">Terms of Service</a> and <a href="privacy:">Privacy Policy.</a></string>
    
  3. 在onCreateView中将LinkMovementMethod设置为TextView

    TextView privacyTextView = (TextView) root.findViewById(R.id.sign_up_privacy);
    privacyTextView.setMovementMethod(LinkMovementMethod.getInstance());
    
  4. 现在TextView链接是可点击的。

    其次,我们需要处理这些点击:

    1. 在我的清单文件中,我为&#34;术语&#34;添加了意图过滤器。和&#34;隐私&#34; 和单实例启动模式

      <activity
                  android:name=".MyActivity"
                  android:launchMode="singleInstance">
                  <intent-filter>
                      <category android:name="android.intent.category.DEFAULT"/>
                      <action android:name="android.intent.action.VIEW"/>
      
                      <data android:scheme="terms"/>
                      <data android:scheme="privacy"/>
                  </intent-filter>
              </activity>
      
    2. 在MyActivity中,覆盖onNewIntent以捕获隐私和术语 意图

      @Override
          protected void onNewIntent(Intent intent)
          {
              if (intent.getScheme().equalsIgnoreCase(getString("terms")))
              {
                  //handle terms clicked
              }
              else if (intent.getScheme().equalsIgnoreCase(getString("privacy")))
              {
                  //handle privacy clicked
              }
              else
              {
                  super.onNewIntent(intent);
              }
          }
      

答案 3 :(得分:1)

最好的方法是使用字符串文件

在string.xml中

<string name="agree_to_terms">I agree to the %1$s and %2$s</string>
<string name="terms_of_service">Terms of Service</string>
<string name="privacy_policy">Privacy Policy</string>

在方法下面的onCreateView调用中

private void setTermsAndCondition() {
    String termsOfServicee = getString(R.string.terms_of_service);
    String privacyPolicy = getString(R.string.privacy_policy);
    String completeString = getString(R.string.agree_to_terms, termsOfServicee,privacyPolicy);
    int startIndex = completeString.indexOf(termsOfServicee);
    int endIndex = startIndex + termsOfServicee.length();
    int startIndex2 = completeString.indexOf(privacyPolicy);
    int endIndex2 = startIndex2 + privacyPolicy.length();
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(completeString);
    ClickableSpan clickOnTerms = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast.makeText(this, "click on terms of service", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(ContextCompat.getColor(this, R.color.blue));

        }
    };
    ClickableSpan clickOnPrivacy = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast.makeText(this, "click on privacy policy", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(ContextCompat.getColor(this, R.color.blue));

        }
    };
    spannableStringBuilder.setSpan(clickOnTerms, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableStringBuilder.setSpan(clickOnPrivacy, startIndex2, endIndex2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    yourTextView.setText(spannableStringBuilder);
    yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
}

答案 4 :(得分:0)

一个简单的解决方法是使用多个标签。每个链接一个,文本一个。

[TermsOfServiceLabel][andLabel][PrivacyPolicy]

或者您只需要使用一个标签吗?

答案 5 :(得分:0)

使用Textoo,可以实现:

RES /值/ strings.xml中:

<resources>
     <string name="str_terms_and_privacy">By signing up you agree to our <a href="terms:">Terms of Service</a> and <a href="privacy:">Privacy Policy.</a></string>
</resources>

RES /布局/ myActivity.xml:

<TextView
    android:id="@+id/view_terms_and_privacy"
    android:text="@string/str_terms_and_privacy"
    />

的java / mypackage的/ MyActivity.java:

public class MyActivity extends Activity {
    ...
    protected void onCreate(Bundle savedInstanceState) {
        ...
        TextView termsAndPrivacy = Textoo
            .config((TextView) findViewById(R.id.view_terms_and_privacy))
            .addLinksHandler(new LinksHandler() {
                @Override
                public boolean onClick(View view, String url) {
                    if ("terms:".equals(url)) {
                        // Handle terms click
                        return true;  // event handled
                    } else if ("privacy:".equals(url)) {
                        // Handle privacy click
                        return true;  // event handled
                    } else {
                        return false;  // event not handled.  continue default processing i.e. launch web browser and display the link
                    }
                }
            })
            .apply();
        ...
    }
    ...
}

这种方法具有以下优点:

  • 将文本外部化为字符串资源。可以更轻松地本地化您的应用。
  • 可以使用LinksHandler直接处理点击事件,无需定义其他意图过滤器
  • 更简单,更易读的代码

答案 6 :(得分:0)


private fun customTextView(view: TextView) {

        //I agree to the Terms of service & Privacy Policy,
        val spanTxt = SpannableStringBuilder(
            "I agree to the ")
        spanTxt.append("Terms of service")
        spanTxt.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
             
                toast("Terms of service link clicked")

            }
        }, spanTxt.length - "Term of services".length, spanTxt.length, 0)
        spanTxt.append(" and")
        spanTxt.append(" Privacy Policy")
        spanTxt.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {

                toast("Privacy Policy link clicked")

            }
        }, spanTxt.length - " Privacy Policy".length, spanTxt.length, 0)
        view.movementMethod = LinkMovementMethod.getInstance()
        view.setText(spanTxt, TextView.BufferType.SPANNABLE)
    }