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);//光标所在位置插入文字
}
* **/
}
}