我正在尝试在文本视图中添加多个链接,类似于 Google 和 Flipboard 在下面所做的操作,其条款和条件和隐私政策如下面的屏幕截图所示:
到目前为止,我偶然发现了这种方法
textView.setText(Html.fromHtml(myHtml); textView.setMovementMethod(LinkMovementMethod.getInstance());
其中 myHtml 是一个 href。
但它没有给我所需的控制权,例如启动片段等。
知道他们如何在下面的两个示例中实现这一目标吗?
我围绕此为 Composable 创建了一个小型库。您可以简单地使用它并节省时间。这是图书馆: TermsText_Composable
我认为我分享这个有点晚了,但我已经使用 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
看起来像这样:
您可以使用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>
如果您想执行自定义操作,您可以将意图过滤器设置为当前活动,该活动将具有单一顶部启动模式。
这将导致 onNewIntent 被触发,可以进行自定义操作:
@Override
protected void onNewIntent(final Intent intent) {
...
if (intent.getScheme().equals(..)) {
..
}
}
这是我的解决方案:
首先我们需要在 TextView 中有可点击的链接:
这是我的TextView在xml布局中,不添加任何链接处理 参数。
<TextView
android:id="@+id/sign_up_privacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/terms_and_privacy"/>
在字符串文件中,我添加了带有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>
在onCreateView中将LinkMovementMethod设置为TextView
TextView privacyTextView = (TextView) root.findViewById(R.id.sign_up_privacy);
privacyTextView.setMovementMethod(LinkMovementMethod.getInstance());
现在 TextView 链接可以单击。
其次,我们需要处理这些点击:
在我的清单文件中,我添加了“术语”和“隐私”的意图过滤器 和单实例启动模式
<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>
在 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);
}
}
最好的方法是使用字符串文件
在 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());
}
使用 Textoo,可以这样实现:
res/values/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/layout/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
处理点击事件,无需定义额外的intent过滤器这对我有用:
在 XML 中:
<TextView
android:id="@+id/tv_by_continuing_str"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textSize="15sp"
tools:text="Test msg 1 2, 3"
android:textColor="@color/translucent_less_white3"
android:textColorLink="@color/white"
android:gravity="center|bottom"
android:layout_above="@+id/btn_privacy_continue" />
在 strings.xml 中
< string name="by_continuing_str2">< ! [ CDATA[By continuing to use this app, you agree to our <a href="https://go.test.com" style="color:gray"/> Privacy Statement </a> and <a href="https://go.test.com" style="color:gray"/>Services Agreement.]]>< / string>
活动中:
TextView tv_by_continuing = (TextView) findViewById(R.id.tv_by_continuing);
tv_by_continuing.setText(Html.fromHtml(getString(R.string.by_continuing_str2)));
tv_by_continuing.setMovementMethod(LinkMovementMethod.getInstance());
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)
}
解决此问题的一个简单方法是使用多个标签。每个链接一个,一个文本。
[TermsOfServiceLabel][andLabel][PrivacyPolicy]
或者有必要只使用一个标签吗?