AndroidUI-TxetView嵌套Html的使用
2015-10-20 10:39
316 查看
TxetView嵌套Html的使用
在Android里面 我们设置组件通常是XML文件进行设计,但是在java代码里面也可以插入Html语言进行嵌套,然而
如果我们不使用Html嵌套的话,要实现跳转超链接就要通过
Intent来实现。
运行效果图:
点击百度一下:
布局文件
activity_main.xml
[code]<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
[code]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 = "<a href='http://www.baidu.com'>百度一下</a>"; 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()); } }
核心代码:
[code]CharSequence charSequence = Html.fromHtml(html); tv1.setText(charSequence); tv1.setMovementMethod(LinkMovementMethod.getInstance());
相关文章推荐
- UITabbar UINavigationBar
- 【最新API翻译】 Bluetooth--BluetoothAdapter API 翻译
- Qt使用教程之创建Qt Quick项目
- 「8-Queens Problem」皇后问题局部极值启发式搜索方法
- UINavigationItem UINavigationBar 关系分析
- STL学习笔记——序列式容器deque
- 浅谈UIScrollView
- VS2005编译问题LINK : E:/aaa/Debug/aaa.exe not found or not built by the last incremental link; performin
- 开速开发:UIView与XIB关联
- easyui 批量 移除行,
- QT小例子GUI(主)线程与子线程之间的通信
- require.js的用法详解
- 【IOS 开发学习总结-OC-59】UI控件——UIPageControl 与UIPageViewController
- UIAlertView 和 UIAlertController
- iOS开发之UITextField常用属性设置
- UILabel,文字添加下划线,中划线
- UISearchBar和导航栏按钮
- iOS中表视图(UITableView)使用详解
- UITextView输入中文时,在输入多行后,光标有时会上下跳动
- 如何解决 错误code signing is required for product type 'xxxxx' in SDK 'iOS 8.2'