我有TextView文本,动态更改.这个文本包含像<a href='myWord'>myWord</a>.我希望在点击这个"链接"之后,myWord出现在同一活动的EditText中.
这是我的代码:
txt.setText(Html.fromHtml("...<a href='link'>link</a>..."));
txt.setMovementMethod(LinkMovementMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)
它适用于href属性中的URL,但是另一种格式存在错误.
我在StackOverflow上发现了很多类似的问题,但所有问题都与url链接有关.在我的应用程序中,我想在活动中创建"链接".一般来说,如果依赖,我可以将标签更改为其他标签...
请帮我!谢谢!
-----已解决-----谢谢雅各布菲利普斯的想法!
将来可能会有人感兴趣.这是一个代码:
//This is my string;
String str = "<b>Text</b> which contains one <a href='#'>link</a> and another <a href='#'>link</a>";
//TextView;
TextView txt = new TextView(this);
//Split string to parts:
String[] devFull = data[v.getId()][1].split("<a href='#'>");
//Adding first part:
txt.append(Html.fromHtml(devFull[0]));
//Creating array for parts with links (they amount always will devFull.length-1):
SpannableString[] link = new SpannableString[devFull.length-1];
//local vars:
ClickableSpan[] cs = new ClickableSpan[devFull.length-1];
String linkWord;
String[] devDevFull = new String[2];
for(int i=1; i<devFull.length; i++){
//obtaining 'clear' link
devDevFull = devFull[i].split("</a>");
link[i-1] = new SpannableString(devDevFull[0]);
linkWord = devDevFull[0];
cs[i-1] = new ClickableSpan(){
private String w = linkWord;
@Override
public void onClick(View widget) {
// here you can use w (linkWord)
}
};
link[i-1].setSpan(cs[i-1], 0, linkWord.length(), 0);
txt.append(link[i-1]);
try{
txt.append(Html.fromHtml(devDevFull[1]));
}
catch(Exception e){}
}
Run Code Online (Sandbox Code Playgroud)
Jac*_*ips 44
这应该可以解决问题.只需更改您的edittext文本即可OnClickListener.它可能会减少,但这应该工作.
private void foo() {
SpannableString link = makeLinkSpan("click here", new View.OnClickListener() {
@Override
public void onClick(View v) {
// respond to click
}
});
// We need a TextView instance.
TextView tv = new TextView(context);
// Set the TextView's text
tv.setText("To perform action, ");
// Append the link we created above using a function defined below.
tv.append(link);
// Append a period (this will not be a link).
tv.append(".");
// This line makes the link clickable!
makeLinksFocusable(tv);
}
/*
* Methods used above.
*/
private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener) {
SpannableString link = new SpannableString(text);
link.setSpan(new ClickableString(listener), 0, text.length(),
SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
return link;
}
private void makeLinksFocusable(TextView tv) {
MovementMethod m = tv.getMovementMethod();
if ((m == null) || !(m instanceof LinkMovementMethod)) {
if (tv.getLinksClickable()) {
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
/*
* ClickableString class
*/
private static class ClickableString extends ClickableSpan {
private View.OnClickListener mListener;
public ClickableString(View.OnClickListener listener) {
mListener = listener;
}
@Override
public void onClick(View v) {
mListener.onClick(v);
}
}
Run Code Online (Sandbox Code Playgroud)
Xar*_*mer 14
更好的方法是
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));
}
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//where 22 and 27 are the starting and ending index of the String. Now word stack is clickable
// onClicking stack it will open NextActiivty
TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)
我所知道的最好的解决方法是创建您自己的 Button 类。您可以使按钮具有透明背景,以便用户只能看到文本。然后,当按下按钮时,将按钮的 TextColor 和 TextStyle 更改为较深的颜色并带有下划线。这将完全像链接一样工作。然后,您可以使用 startActivity 转到相应的活动。您不应使用超链接来连接到应用程序中的其他活动。
| 归档时间: |
|
| 查看次数: |
20632 次 |
| 最近记录: |