asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
package com.asfman.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.widget.TextView;
public class Spannable extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.cao);
String htmlLinkText = "";
htmlLinkText = "aaaaaaaaaa"
+ "<a style=\"color:red;\" href=\"http://g.cn\">超链接点击<sup>事件</sup></a>"
+ "aaaaaaaaaaaaaaaa";
Log.i("info", Html.fromHtml(htmlLinkText).toString());
tv.setText(Html.fromHtml(htmlLinkText));
SpannableStringBuilder ss = null;
ss = new SpannableStringBuilder("红色打电话斜体删除线绿色下划线背景");
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
///URLSpan URLSpan("http://g.cn")
ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new StrikethroughSpan(), 7, 10,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new UnderlineSpan(), 10, 16,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 15,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new BackgroundColorSpan(Color.#7c9cd7), 15, 17,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//----------------------------
Pattern pattern = Pattern.compile("绿色", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("红色打电话斜体删除线绿色下划线背景");
while(matcher.find()) {
ss.setSpan(new ImageSpan(this, R.drawable.icon, ImageSpan.ALIGN_BOTTOM), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.append(ss);
//----------------------------
ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
// 根据id从资源文件中获取图片对象
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
Log.i("info", Html.fromHtml("aa<img src='"+R.drawable.icon+"'/>bb").toString());
tv.append(Html.fromHtml("aa<img src='"+R.drawable.icon+"'/>bb", imageGetter, null));  
Log.i("info", tv.getText().toString());
tv.setMovementMethod(LinkMovementMethod.getInstance());
/**
* //Android EditText插入字符串到光标所在位置
EditText mTextInput=(EditText)findViewById(R.id.input);//EditText对象
int index = mTextInput.getSelectionStart();//获取光标所在位置
String text="I want to input str";
(方法1):
Editable edit = mTextInput.getEditableText();//获取EditText的文字
if (index < 0 || index >= edit.length() ){
      edit.append(text);
}else{
     edit.insert(index,text);//光标所在位置插入文字
}
* **/
}
}
posted on 2011-06-30 20:52 汪杰 阅读(680) 评论(0)  编辑 收藏 引用 所属分类: Java
只有注册用户登录后才能发表评论。

<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 469034
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜