textview-Android 的TextView的点击事件如何去除
1个回答
展开全部
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 {
/* * Called when the activity is first created. */
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=\\" +"<a href="http://www.baidu.com/><u">http://www.baidu.com/><u</a>>我的CSDN博客 </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){
MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
style.setSpan(myURLSpan,sp.getSpanStart(url),sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(style);
}
}
private static class MyURLSpan 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! " ,Toast.LENGTH_LONG).show();
}
}
}
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 {
/* * Called when the activity is first created. */
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=\\" +"<a href="http://www.baidu.com/><u">http://www.baidu.com/><u</a>>我的CSDN博客 </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){
MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
style.setSpan(myURLSpan,sp.getSpanStart(url),sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(style);
}
}
private static class MyURLSpan 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! " ,Toast.LENGTH_LONG).show();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询