我怎样才能打开的TextView
的链接进入的WebView
当我点击的TextView 。
跨区跨区= Html.fromHtml(< A HREF = \的http://google.com \ > google.com&所述; / A>中);
textView.setText(跨越);
修改:这不是一个理想的方法来处理点击一个链接,但我不知道任何其他方式
您的主要活动包含了一个的TextView
的链接。链接URL有一个自定义模式。
公共类MainActivity延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
TextView的链接=(TextView中)findViewById(R.id.link);
link.setText(
Html.fromHtml(&所述; A HREF =myscheme://www.google.com'>链路其中; / A>中));
link.setMovementMethod(LinkMovementMethod.getInstance());
}
}
在这个链接被点击的Android使用的链接URL启动一个活动
与 ACTION_VIEW
。让我们假设你有一个 WebViewActivity
它处理的URI这个自定义方案。它获得通过URI和替换其计划与 HTTP
。
公共类WebViewActivity延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
如果(savedInstanceState == NULL){
字符串URL =
。getIntent()getDataString()代替(myscheme://,HTTP://);
//做一些与此网址。
}
}
}
要处理自定义的URI方案 WebViewActivity
必须在的AndroidManifest.xml
文件的意图过滤器:
<活动机器人:WebViewActivity名称=机器人:出口=假>
<意向滤光器>
<作用机器人:名称=android.intent.action.VIEW/>
<类机器人:名称=android.intent.category.DEFAULT/>
<数据机器人:计划=myscheme/>
&所述; /意图滤光器>
< /活性GT;
How can I open TextView
's links into WebView
when I click on links of TextView
.
Spanned spanned = Html.fromHtml("<a href=\"http://google.com\">google.com</a>");
textView.setText(spanned);
EDIT: That's not an ideal way to handle clicks on a link, but I don't know any other way.
Your main activity contains a TextView
with a link. The link URL has a custom scheme.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView link = (TextView)findViewById(R.id.link);
link.setText(
Html.fromHtml("<a href='myscheme://www.google.com'>link</a>"));
link.setMovementMethod(LinkMovementMethod.getInstance());
}
}
When this link is clicked Android starts an Activity
with ACTION_VIEW
using the link URL. Let's assume you have a WebViewActivity
which handles URIs with this custom scheme. It gets the passed URI and replaces its scheme with http
.
public class WebViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
if( savedInstanceState == null ) {
String url =
getIntent().getDataString().replace("myscheme://", "http://");
// do something with this URL.
}
}
}
To handle custom URI schemes WebViewActivity
must have an intent filter in the AndroidManifest.xml
file:
<activity android:name=".WebViewActivity" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="myscheme" />
</intent-filter>
</activity>