我有这段代码:
TextView noteView = (TextView) view.findViewById(R.id.content);
noteView.setMovementMethod(LinkMovementMethod.getInstance());
noteView.setText(Html.fromHtml(noteView.getText().toString()));
我需要在webview中打开链接,而不是在浏览器中打开...这可能吗?我该怎么办?
提前致谢..
答案 0 :(得分:7)
另外,你可以这样做。
package com.TextHtml;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class TextHtml extends Activity {
private TextView tv;
static Context ctx=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ctx=this;
tv = (TextView) findViewById(R.id.tv);
String htmlLinkText = "<a href="/" mce_href="/""http://www.google.com/"><u>hello google </u></a>";
tv.setText(Html.fromHtml(htmlLinkText));
tv.setMovementMethod(LinkMovementMethod.getInstance());
CharSequence text = tv.getText();
if(text instanceof Spannable){
int end = text.length();
Spannable sp = (Spannable)tv.getText();
URLSpan[] urls=sp.getSpans(0, end, URLSpan.class);
SpannableStringBuilder style=new SpannableStringBuilder(text);
style.clearSpans();//should clear old spans
for(URLSpan url : urls){
CustomerTextClick click = new CustomerTextClick(url.getURL());
style.setSpan(click,sp.getSpanStart(url),sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(style);
}
}
private static class CustomerTextClick extends ClickableSpan{
private String mUrl;
MyURLSpan(String url) {
mUrl =url;
}
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
Toast.makeText(ctx, "hello google!",Toast.LENGTH_LONG).show();
}
}
}
答案 1 :(得分:5)
是的,您可以这样做,使用WebView这是非常简单的任务,您需要声明一个WebViewClient对象并覆盖public boolean shouldOverrideUrlLoading (WebView view, String url)
方法,您可以在那里过滤网址或提供一些自定义功能。< / p>
在您的情况下,要继续使用WebView,您需要在该方法上返回false。
此致
已编辑:
看来你的问题是如何处理TextView网址上的click事件。作为it's suggested on this question,您可以过滤包含Activity的WebView上的ACTION_VIEW
意图。如果您需要more guidance about intent-filters, check this out。