Android:将 onClickListener 设置为 TextView 中的部分文本 - 问题
Posted
技术标签:
【中文标题】Android:将 onClickListener 设置为 TextView 中的部分文本 - 问题【英文标题】:Android: Setting onClickListener to a Part of text in a TextView - Issue 【发布时间】:2015-12-31 05:06:24 【问题描述】:我正在尝试识别我的 TextView 中的主题标签并使其可点击,以便当用户单击该主题标签时我可以将他们带到另一个视图。
我设法使用模式匹配在 TextView 中识别 Hashtags,它们在运行时显示为彩色。但是,我需要使 Hashtag 可点击。
这是我的代码:
SpannableString hashText = new SpannableString("I just watched #StarWars and it was incredible. It's a #MustWatch #StarWars");
Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashText);
while (matcher.find())
hashText.setSpan(new ForegroundColorSpan(Color.parseColor("#000763")), matcher.start(), matcher.end(), 0);
String tag = matcher.group(0);
holder.caption.setText(hashText);
//I need to set an OnClick listener to all the Hashtags recognised
使用上述相同的解决方案,如何将 onclick 侦听器添加到每个主题标签?
【问题讨论】:
有图书馆可以做同样的事情,看看这个:github.com/Danylo2006/HashTagHelper @SachinRao 看起来该库不适用于最低 sdk 9 :( 尝试将 2 个 java 文件粘贴到您的任何包中。 (HashTagHelper 和 ClickableForegroundColorSpan) 【参考方案1】:有一种方法......在看到你的问题后,我只是在谷歌搜索......我发现了这个,我希望它会工作......
1。
你可以使用android.text.style.ClickableSpan
link
SpannableString ss = new SpannableString("Hello World");
ClickableSpan span1 = new ClickableSpan()
@Override
public void onClick(View textView)
// do some thing
;
ClickableSpan span2 = new ClickableSpan()
@Override
public void onClick(View textView)
// do another thing
;
ss.setSpan(span1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(span2, 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
另一种方式..link
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);
刚刚从那些链接复制的答案...
【讨论】:
【参考方案2】:是的,你可以做到,你需要使用ClickableSpan
和SpannableString
将此代码粘贴到您的 while 循环中
final String tag = matcher.group(0);
ClickableSpan clickableSpan = new ClickableSpan()
@Override
public void onClick(View textView)
Log.e("click","click " + tag);
@Override
public void updateDrawState(TextPaint ds)
super.updateDrawState(ds);
;
hashText.setSpan(clickableSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
别忘了在你的TextView
上设置setMovementMethod()
holder.caption.setMovementMethod(LinkMovementMethod.getInstance());
【讨论】:
【参考方案3】:您可以使用此代码通过 textview 中的可点击任意键(@,#)文本更改颜色。我使用此格式。它在我身边很好用。
private void setTags(TextView pTextView, String pTagString)
SpannableString string = new SpannableString(pTagString);
int start = -1;
for (int i = 0; i < pTagString.length(); i++)
if (pTagString.charAt(i) == '@' || pTagString.charAt(i) == '#')
start = i;
else if (pTagString.charAt(i) == ' '
|| (i == pTagString.length() - 1 && start != -1))
if (start != -1)
if (i == pTagString.length() - 1)
i++; // case for if hash is last word and there is no
// space after word
final String tag = pTagString.substring(start, i);
string.setSpan(new ClickableSpan()
@Override
public void onClick(View widget)
Toast.makeText(mContext, "" + tag, 3000)
.show();
@Override
public void updateDrawState(TextPaint ds)
if (tag.contains("@"))
ds.setColor(Color.parseColor("#0474be"));
else
ds.setColor(Color.parseColor("#ed6057"));
ds.setUnderlineText(false);
, start, i, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = -1;
pTextView.setMovementMethod(LinkMovementMethod.getInstance());
pTextView.setText(string);
请尝试这种方式希望可以解决您的问题。
【讨论】:
【参考方案4】://得到但没有模式匹配
SpannableString hashText = new SpannableString("I just watched #StarWars and it was incredible. It's a #MustWatch #StarWars");
ClickableSpan clickableSpanstar1stWarsHashText = new ClickableSpan()
@Override
public void onClick(View widget)
//Intent starWars = new Intent(MainActivity.this,starWars.class); //starWars is a class file which extends Activity
//startActivity(starWars);
Toast.makeText(MainActivity.this,"Clicked On 1st #StarWars Remove comments of above line",Toast.LENGTH_LONG).show();
;
ClickableSpan clickableSpanmustWatchHashText = new ClickableSpan()
@Override
public void onClick(View widget)
//Intent mustWatch = new Intent(MainActivity.this,mustWatch.class); //starWars is a class file which extends Activity
//startActivity(mustWatch);
Toast.makeText(MainActivity.this,"Clicked On #MustWatch Remove comments of above line",Toast.LENGTH_LONG).show();
;
ClickableSpan clickableSpanstar2ndWarsHashText = new ClickableSpan()
@Override
public void onClick(View widget)
//Intent starWars = new Intent(MainActivity.this,starWars.class); //starWars is a class file which extends Activity
//startActivity(starWars);
Toast.makeText(MainActivity.this,"Clicked On 2nd #StarWars Remove comments of above line",Toast.LENGTH_LONG).show();
;
hashText.setSpan(clickableSpanstar1stWarsHashText,15,24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
hashText.setSpan(clickableSpanmustWatchHashText, 55, 65, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
hashText.setSpan(clickableSpanstar2ndWarsHashText,66,75, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//SpannableString hashText = new SpannableString("I just watched #StarWars and it was incredible. It's a #MustWatch #StarWars");
TextView textView = (TextView) findViewById(R.id.textView); //textView id i.e. android:id="@+id/textView"
textView.setText(hashText);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
【讨论】:
【参考方案5】:您可以为此使用android.text.style.ClickableSpan
:
参考answer
【讨论】:
【参考方案6】:您可以通过使用split
函数并在Layout
中填充textview's
来实现它,如下所示:
layout=(LinearLayout)findViewById(R.id.layout);
String data="I just watched #StarWars and it was incredible. It's a #MustWatch #StarWars";
TextView textView;
String [] s=data.split(" ");
for(int i=0;i<s.length;i++)
if(s[i].matches("#([A-Za-z0-9_-]+)"))
textView=new TextView(this);
textView.setText(s[i]);
textView.setTextColor(Color.parseColor("#000763"));
textView.setTag(s[i]);
textView.setOnClickListener(viewClicked(textView));
else
textView=new TextView(this);
textView.setText(" "+s[i]);
layout.addView(textView,i);
以及处理点击事件到所需Textview's
的方法:
View.OnClickListener viewClicked(final TextView textView)
return new View.OnClickListener()
public void onClick(View v)
Toast.makeText(getApplicationContext(), v.getTag().toString(), 1000).show();
;
【讨论】:
【参考方案7】:我遇到了类似的问题——我不得不对点击的部分文本做某事,但字符串是在运行时创建的。也许它会对某人有所帮助 - 在这个解决方案中,我们不关心在整个文本中找到 indexStart、indexEnd(这可能非常糟糕)等:
val action1ClickedSpan = object : ClickableSpan()
override fun onClick(widget: View?)
presenter.action1Clicked()
val possibleActionsHint = SpannableStringBuilder().apply
val action1Start = this.length
append(getString(R.string.action1))
val action2Start = this.length
append(...)
append(...)
val action2Start = this.length
append(getString(R.string.action1))
val action2End = this.length
setSpan(action1ClickedSpan, action1Start, action1End, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
setSpan(...)
this.possibleActionsTextView.apply
text = possibleActionsHint
movementMethod = LinkMovementMethod.getInstance()
【讨论】:
以上是关于Android:将 onClickListener 设置为 TextView 中的部分文本 - 问题的主要内容,如果未能解决你的问题,请参考以下文章
Android:如果我将 Switch OnClickListener 放在 FragmentActivity 上,我的应用程序将无法运行
Android OnItemClickListener 和 OnClickListener [重复]
Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法
Android OnClickListener 多个视图? [复制]