TxetView嵌套Html的使用


在Android里面 我们设置组件通常是XML文件进行设计,
但是在java代码里面也可以插入Html语言进行嵌套,然而
如果我们不使用Html嵌套的话,要实现跳转超链接就要通过
Intent来实现。
运行效果图:

点击百度一下:


布局文件
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/tv1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="20sp" />    <TextView        android:id="@+id/tv2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:autoLink="all" />LinearLayout>

MainActivity.java

package com.example.textviewdemo;import android.app.Activity;import android.os.Bundle;import android.text.Html;import android.text.method.LinkMovementMethod;import android.widget.TextView;public class MainActivity extends Activity {    private TextView tv1 = null;    private TextView tv2 = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv1 = (TextView) findViewById(R.id.tv1);        tv2 = (TextView) findViewById(R.id.tv2);        String html = "百度一下";        CharSequence charSequence = Html.fromHtml(html);        tv1.setText(charSequence);        tv1.setMovementMethod(LinkMovementMethod.getInstance());        String text = "我的微博:http://www.sina.com";        tv2.setText(text);        tv2.setMovementMethod(LinkMovementMethod.getInstance());    }}

核心代码:

CharSequence charSequence = Html.fromHtml(html);tv1.setText(charSequence);tv1.setMovementMethod(LinkMovementMethod.getInstance());

更多相关文章

  1. Android学习笔记(三):Andriod程序框架
  2. 在代码中实现按下Home键的效果
  3. android中设置控件边框以及如何保留上或下边框
  4. 【Android(安卓)Debug】 Skipping insecure file ...
  5. webView获取链接后的url和加载经过处理后的HTML
  6. ANDROID应用程序的混淆打包分享
  7. 如何解决软键盘弹出引起的各种不适
  8. OpenCV4Android(安卓)之 OpenCV4Android(安卓)SDK
  9. Android之监测database的改变--notifyChange

随机推荐

  1. android push
  2. android音频处理
  3. Android中AutoCompleteTextView的特殊使
  4. Android(安卓)修改U盘名称
  5. Android与蓝牙耳机建立连接的分析
  6. Android(安卓)点击back键两次退出程序
  7. 一、 Android完全退出应用程序
  8. Android(安卓)NDK编程实现终端功能(调用sy
  9. 50个常用sql语句 网上流行的学生选课表的
  10. SQL Server 数据库索引其索引的小技巧