如何设置文本视图的部分是可点击的
我有文字“ Android是一个软件堆栈 ”。 在这个文本中,我想设置“ 堆栈 ”文本是可点击的。 从某种意义上说,如果你点击它将被redirect到一个新的活动(不在浏览器中)。
我试过,但我没有得到。
android.text.style.ClickableSpan
可以解决你的问题。
SpannableString ss = new SpannableString("Android is a Software stack"); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View textView) { startActivity(new Intent(MyActivity.this, NextActivity.class)); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } }; ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); TextView textView = (TextView) findViewById(R.id.hello); textView.setText(ss); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setHighlightColor(Color.TRANSPARENT);
//在XML中:TextView:android:textColorLink =“@ drawable / your_selector”
您可以按照本文所述使用ClickableSpan
示例代码:
TextView myTextView = new TextView(this); String myString = "Some text [clickable]"; int i1 = myString.indexOf("["); int i2 = myString.indexOf("]"); myTextView.setMovementMethod(LinkMovementMethod.getInstance()); myTextView.setText(myString, BufferType.SPANNABLE); Spannable mySpannable = (Spannable)myTextView.getText(); ClickableSpan myClickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { /* do something */ } }; mySpannable.setSpan(myClickableSpan, i1, i2 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
参考
你可以使用示例代码。 您想了解有关ClickableSpan的详细信息。 请检查这个文件
SpannableString myString = new SpannableString("This is example"); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View textView) { ToastUtil.show(getContext(),"Clicked Smile "); } }; //For Click myString.setSpan(clickableSpan,startIndex,lastIndex,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) //For UnderLine myString.setSpan(new UnderlineSpan(),startIndex,lastIndex,0); //For Bold myString.setSpan(new StyleSpan(Typeface.BOLD),startIndex,lastIndex,0); //Finally you can set to textView. TextView textView = (TextView) findViewById(R.id.txtSpan); textView.setText(myString); textView.setMovementMethod(LinkMovementMethod.getInstance());
我在TextView
创build多个链接的function
public void makeLinks(TextView textView, String[] links, ClickableSpan[] clickableSpans) { SpannableString spannableString = new SpannableString(textView.getText()); for (int i = 0; i < links.length; i++) { ClickableSpan clickableSpan = clickableSpans[i]; String link = links[i]; int startIndexOfLink = textView.getText().toString().indexOf(link); spannableString.setSpan(clickableSpan, startIndexOfLink, startIndexOfLink + link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setText(spannableString, TextView.BufferType.SPANNABLE); }
使用
TextView myTextView = (TextView) findViewById(R.id.myText); ClickableSpan termsOfServicesClick = new ClickableSpan() { @Override public void onClick(View view) { Toast.makeText(getApplicationContext(), "Terms of services Clicked", Toast.LENGTH_SHORT).show(); } }; ClickableSpan privacyPolicyClick = new ClickableSpan() { @Override public void onClick(View view) { Toast.makeText(getApplicationContext(), "Privacy Policy Clicked", Toast.LENGTH_SHORT).show(); } }; makeLinks(myTextView, new String[] { "Term of services", "Privacy Policy" }, new ClickableSpan[] { termsOfServicesClick, privacyPolicyClick });
XML
<TextView android:id="@+id/myText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Please accept Term of services and Privacy Policy" android:textColorLink="#00f" android:textColorHighlight="#00ffffff" />
t= (TextView) findViewById(R.id.PP1); t.setText(Html.fromHtml("<bThis is normal text </b>" + "<a href=\"http://www.xyz-zyyx.com\">This is cliclable text</a> ")); t.setMovementMethod(LinkMovementMethod.getInstance());
我做了这个帮手方法,以防有人需要从string开始和结束位置。
public static TextView createLink(TextView targetTextView, String completeString, String partToClick, ClickableSpan clickableAction) { SpannableString spannableString = new SpannableString(completeString); // make sure the String is exist, if it doesn't exist // it will throw IndexOutOfBoundException int startPosition = completeString.indexOf(partToClick); int endPosition = completeString.lastIndexOf(partToClick) + partToClick.length(); spannableString.setSpan(clickableAction, startPosition, endPosition, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); targetTextView.setText(spannableString); targetTextView.setMovementMethod(LinkMovementMethod.getInstance()); return targetTextView; }
这是你如何使用它
private void initSignUp() { String completeString = "New to Reddit? Sign up here."; String partToClick = "Sign up"; ClickableTextUtil .createLink(signUpEditText, completeString, partToClick, new ClickableSpan() { @Override public void onClick(View widget) { // your action Toast.makeText(activity, "Start Sign up activity", Toast.LENGTH_SHORT).show(); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); // this is where you set link color, underline, typeface etc. int linkColor = ContextCompat.getColor(activity, R.color.blumine); ds.setColor(linkColor); ds.setUnderlineText(false); } }); }
为了大胆,
mySpannable.setSpan(new StyleSpan(Typeface.BOLD),termStart,termStop,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);