[英]How to make one word in a TextView with a long text from a string clickable?
I have a little question about coding in Java on Android. 我对Android上的Java编码存在一些疑问。
So, I have one TextView with a long text from a string. 因此,我有一个TextView,其中包含来自字符串的长文本。 I want to make only one word of this long text clickable, that I can link it to an other Activity.
我只想使这个长文本中的一个单词可点击,就可以将其链接到另一个活动。
By the way, I'm new to the Android development, so please don't explain your solution too complicated. 顺便说一下,我是Android开发的新手,所以请不要解释您的解决方案过于复杂。 Thanks!
谢谢! :)
:)
I have no idea, how I could do this. 我不知道该怎么做。 I also googled after a solution before but didn't find a clear way that worked well.
我也曾在解决方案前搜索过Google,但没有找到一种行之有效的明确方法。 Anyway, I would appreciate your help.
无论如何,我将感谢您的帮助。
What is the outcome you want for the click? 您想要点击的结果是什么?
The simplest way to do this would be to apply a URLSpan
onto the TextView's contents, but if you want to do something other than view a webpage you can implement your own version of ClickableSpan
and make the click do whatever you want. 最简单的方法是将
URLSpan
应用于TextView的内容,但是,如果要执行其他操作而不是查看网页,则可以实现自己的ClickableSpan
版本,并使点击操作随心所欲。
Edit per your comment: 根据您的评论进行编辑:
Making a ClickableSpan go to another activity is really easy, here's the start of the code you'd need for it: 使ClickableSpan转到另一个活动真的很容易,这是您需要的代码的开始:
public class MyURLSpan extends ClickableSpan {
Class<?> mClassToLaunch;
public MyURLSpan(Class<?> classToLaunch) {
mClassToLaunch = classToLaunch;
}
@Override
public void onClick(View widget) {
Intent intent = new Intent(widget.getContext(), mClassToLaunch);
widget.getContext().startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds) {
// If you don't want the default drawstate (blue-underline), comment this super call out.
super.updateDrawState(ds);
}
}
Then to use it: 然后使用它:
String longText = "your text goes here...";
SpannableString textViewContents = new SpannableString(longText);
textViewContents.setSpan(new MyURLSpan(NextActivity.class), indexToStart, lengthOfClickArea, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(textViewContents);
You can create a TextView per-word, and add them to a FlowLayout. 您可以创建每个单词一个TextView,并将它们添加到FlowLayout中。 Then you can assign OnClick events to any view you want.
然后,您可以将OnClick事件分配给所需的任何视图。
The sentance "This is a test" would actually be made up of 4 TextViews, all inside some Layout (maybe a FlowLayout). 感觉“这是一个测试”实际上将由4个TextView组成,全部位于某个Layout(可能是FlowLayout)中。
There's some details about a FlowLayout in this SO post: 在此SO帖子中有关于FlowLayout的一些详细信息:
How can I do something like a FlowLayout in Android? 如何在Android中执行类似FlowLayout的操作?
i did not try this but i think if you use below hint then may be You will success. 我没有尝试过,但我认为如果您使用以下提示,那么可能会成功。 txt.setText(Html.fromHtml("...link..."));
txt.setText(Html.fromHtml( “...链接...”)); txt.setMovementMethod(LinkMovementMethod.getInstance());
txt.setMovementMethod(LinkMovementMethod.getInstance());
select a word on a tap in TextView/EditText 在TextView / EditText中点击选择一个单词
i think it will help. 我认为这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.