如何设置文本视图的部分是可点击的
我有文字“ Android是一个软件堆栈 ”。 在这个文本中,我想设置“ 堆栈 ”文本是可点击的。 从某种意义上说,如果你点击它将被重定向到一个新的活动(不在浏览器中)。
我试过,但我没有得到。
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);
参考
我在TextView
创建多个链接的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
你可以使用示例代码。 您想了解有关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());
t= (TextView) findViewById(R.id.PP1); t.setText(Html.fromHtml("" + "This is cliclable text ")); t.setMovementMethod(LinkMovementMethod.getInstance());
我做了这个帮手方法,以防有人需要从字符串开始和结束位置。
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);
潘范林的回答的Kotlin版本 。
请注意它有一些小的修改。
fun makeLinks(textView: TextView, links: Array, clickableSpans: Array) { val spannableString = SpannableString(textView.text) for (i in links.indices) { val clickableSpan = clickableSpans[i] val link = links[i] val startIndexOfLink = textView.text.indexOf(link) spannableString.setSpan(clickableSpan, startIndexOfLink, startIndexOfLink + link.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) } textView.movementMethod = LinkMovementMethod.getInstance() textView.setText(spannableString, TextView.BufferType.SPANNABLE) } fun setupClickableTextView() { val termsOfServicesClick = object : ClickableSpan() { override fun onClick(p0: View?) { Toast.makeText(applicationContext, "ToS clicked", Toast.LENGTH_SHORT).show() } } val privacyPolicyClick = object : ClickableSpan() { override fun onClick(p0: View?) { Toast.makeText(applicationContext, "PP clicked", Toast.LENGTH_SHORT).show() } } makeLinks(termsTextView, arrayOf("terms", "privacy policy"), arrayOf(termsOfServicesClick, privacyPolicyClick)) }
你可以用这个方法设置可点击的值
public void setClickableString(String clickableValue, String wholeValue, TextView yourTextView){ String value = wholeValue; SpannableString spannableString = new SpannableString(value); int startIndex = value.indexOf(clickableValue); int endIndex = startIndex + clickableValue.length(); spannableString.setSpan(new ClickableSpan() { @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); // <-- this will remove automatic underline in set span } @Override public void onClick(View widget) { // do what you want with clickable value } }, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); yourTextView.setText(spannableString); yourTextView.setMovementMethod(LinkMovementMethod.getInstance()); // <-- important, onClick in ClickableSpan won't work without this }
这是如何使用它:
TextView myTextView = findViewById(R.id.myTextView); setClickableString("stack", "Android is a Software stack", myTextView);
- 无法获取任务的未知属性'outputFile':app:packageDebug'
- Kapt3:java.io.File不能转换为java.lang.String
- 如何滚动到RecyclerView的底部? scrollToPosition不起作用
- Android SDK类上的Kotlin扩展函数的单元测试
- 用android studio显示来自MySQL数据库的数据
- 用Kotlin反射调用类的构造函数
- 调用Rooms inMemoryBuilder方法时,Room Persistence Library运行时异常
- 在库模块中使用Kotlin,而不在app模块中使用它
- Dagger2 + MVP上Kotlin