public class MainActivity extends Activity {
private TextView textView1, textView2, textView3;
//通过文件名称来获取这个文件中的 id。
public int getResourceId(String name) {
try {
// 根据资源的ID获得Field对象,使用反射机制
Field field = R.drawable.class.getField(name);
// 获得并返回资源ID的字段(静态变量的)值,使用反射机制。
return Integer.parseInt(field.get(null).toString());
} catch (Exception e) {
// TODO: handle exception
}
return 0;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) this.findViewById(R.id.textView2);
textView2 = (TextView) this.findViewById(R.id.textView3);//在布局中设置了autoLink=“all”,文字有链接等就可以调用系统的。
textView3 = (TextView) this.findViewById(R.id.textView1);
String html = "<font color='red'>O love </font><br>";
html += "<font color='#0000ff'><big><i>o lvoe</i></big></font><p>";
html += "<big><a href ='http://www.baidu.com'>baidu</a></big>";
CharSequence charSequence = Html.fromHtml(html);
textView1.setText(charSequence);
textView1.setMovementMethod(LinkMovementMethod.getInstance());
String text = "URL链接是 :http://www.sina.com\n";
text += "email:abde@126.com\n";
text += "电话 : +86 1234567890";
textView2.setText(text);
textView2.setMovementMethod(LinkMovementMethod.getInstance());
String html1 = "图像一:<a href='http://www.baidu.com'><img src='ic_launcher'></a>";//注意src中输入的不能带文件后缀名。
CharSequence charSequence2=Html.fromHtml(html1, new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
//获得系统资源的信息.
Drawable drawable=getResources().getDrawable(getResourceId(source));//根据返回的id获取资源信息。
if (source.equals("ic_launcher")) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth()/2, drawable.getIntrinsicHeight()/2);
}else {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}
return drawable;
}
}, null);
textView3.setText(charSequence2);
textView3.setMovementMethod(LinkMovementMethod.getInstance());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
实现activity跳转是如下代码
textView4 = (TextView) this.findViewById(R.id.textView4);
String textString = "显示activity";
// 主要是用来拆分字符串,实现点这部分文字的任何一点都可以触发。
SpannableString spannableString = new SpannableString(textString);
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, second.class);
startActivity(intent);
}
}, 0, textString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView4.setText(spannableString);
textView4.setMovementMethod(LinkMovementMethod.getInstance());
接下来是实现跑马灯效果,文字过多实现文字来回移动,并且可以实现超链接。
布局中需要添加的额外属性有
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="53dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="实现跑马灯效果" />
<!--
android:ellipsize = "end" 省略号在结尾
android:ellipsize = "start" 省略号在开头
android:ellipsize = "middle" 省略号在中间
android:ellipsize = "marquee" 跑马灯
android:marqueeRepeatLimit="marquee_forever" 一直在执行跑马灯效果
-->
activity中代码:
textView5 = (TextView) this.findViewById(R.id.textView5);
String htmlPaoMaDeng = "<a href='http://www.baidu.com'>baidu</a>welcome to android's city,i'm android body,hehe!!!!!!!";
CharSequence ch = Html.fromHtml(htmlPaoMaDeng);
textView5.setText(ch);
textView5.setMovementMethod(LinkMovementMethod.getInstance());
这样就可以实现android的跑马灯效果,并且在其中加入超链接。