public class MainActivity extends Activity {
TextView tv1, tv2, tv3, tv4, tv5;
String s, s2, s3, s4, s5;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
// TODO Auto-generated method stub
//实例1,textView点击超链接
s="主页:https://www.baidu.com\n";
s+="个人邮箱:123456789@qq.com\n";
s+="电话:029-12345678";
tv1.setText(s);
//实例2,textView显示Html
s2="<font code='red'>我的浏览器主页</font><br>";
s2+="<a href='https://www.baidu.com'>百度</a>";
Spanned spanned= Html.fromHtml(s2);
tv2.setText(spanned);
tv2.setMovementMethod(LinkMovementMethod.getInstance());
//实例3,使用html插入图片
s3="图片1:<img src='s1'/><br>";
s3+="图片2:<img src='s2'/><br>";
s3+="图片3:<img src='s3'/>";
Spanned s2=Html.fromHtml(s3, new ImageGetter(){
@Override
public Drawable getDrawable(String arg0) {
Drawable d=null;
try {
Field f=R.drawable.class.getField(arg0);
int i=Integer.parseInt(f.get(null).toString());//这里是将图片的信息先转换成string,在转换成int
d=getResources().getDrawable(i);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());//调用方法,使图片显示出来;最后两个参数的意思是图片的宽和高
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
catch (NumberFormatException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return d;
}}, null);
tv3.setText(s2);
//实例4:部分文字点击
s4="点击【这里】显示Toast";
//拆分器
SpannableString ss=new SpannableString(s4);
//第一个参数代表:拆分出来的干什么用,第二个参数、第三个参数代表拆分的那部分,第四个参数是拆分器的选项
ss.setSpan(new ClickableSpan() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "您点击了这里", Toast.LENGTH_SHORT).show();
}
}, 3, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(Color.parseColor("#00ff00")), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv4.setText(ss);
tv4.setMovementMethod(LinkMovementMethod.getInstance());
//实例5:走马灯效果
/*
* android:singleLine="true"单行显示
* android:ellipsize="marquee"当文字长度超过textview宽度时的显示方式,这里是当超过屏幕宽度的时候,后面用省略号显示
* android:marqueeRepeatLimit="marquee_forever"滚动的次数
* android:focusable="true" 可以聚焦的,
* android:focusableInTouchMode="true" 可以聚焦的
*/
s5=".setMovementMethod,此方法在需要响应用户事件时使用,如点击一个电话号码就跳转到拨号页面。如果不执行这个方法是不会响应事件的,即便文本看着已经是下划线蓝色字了。 .Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,这是在 setSpan 时需要指定的 flag,它的意义我试了很久也没试出来,睡个觉,今天早上才突然有点想法,试之,果然。它是用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果。分别有 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)、Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)、Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)、Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)";
tv5.setText(s5);
}
private void initView() {
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
tv5 = (TextView) findViewById(R.id.textView5);
}
只是布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
/>
</LinearLayout>