以下代码正在运行,但我希望链接在Webview中打开。我怎么做?我创建了一个名为WebbVy.java的webview活动,但我猜我需要在下面的textview之后创建一个启动webview活动的intent。但是我该怎么做?目前,textview由title,pubdate,description和link填充(在浏览器中打开),我需要在webview中打开链接而不是浏览器。
VisaHelaNyheten.java
package se.sebastianliljegren.rss_feed;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class VisaHelaNyheten extends Activity{
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.visa_hela_nyhet);
String enNyhet = null;
String mLink = null;
final Intent bundleintent = getIntent();
if (bundleintent != null)
{
Bundle b = bundleintent.getBundleExtra("BUNDLE.VISA.SEN.INTENT");
if (b == null)
{
enNyhet = "fel bundle?";
}
else
{
enNyhet = b.getString("title") + "\n\n" + b.getString("pubdate") + "\n\n" + b.getString("description") + "\n" + "\n\nMore information:\n" + b.getString("link");
mLink = b.getString("link");
}
}
else
{
enNyhet = "Information ej hittad.";
}
TextView db = (TextView) findViewById(R.id.nyhet);
db.setText(enNyhet);
TextView link = (TextView) findViewById(R.id.link);
link.setText(mLink);
link.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(VisaHelaNyheten.this, WebbVy.class);
intent.putExtra("LINK" mLink);
startActivity(intent);
}
});
}
}
WebbVy.java 包se.sebastianliljegren.rss_feed;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@SuppressLint("SetJavaScriptEnabled")
public class WebbVy extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent intent = getIntent();
String link = intent.getStringExtra("link");
WebView mainWebView = (WebView) findViewById(R.id.webview1);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadData(link, "text/html; charset=utf-8","utf-8");
}
private class MyCustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
webview.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview1" >
</WebView>
答案 0 :(得分:0)
使用单独的TextView
来存储以下链接:
String enNyhet = null;
String mLink = null
enNyhet = b.getString("title") + "\n\n" + b.getString("pubdate") + "\n\n" + b.getString("description") + "\n";
mLink = b.getString("link");
TextView db = (TextView) findViewById(R.id.nyhet);
db.setText(enNyhet);
TextView link = (TextView) findViewById(R.id.link);
link.setText(mLink);
现在将onClickListener
添加到链接TextView
:
link.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Set the intent to your webview activity and put the string link as extra.
Intent intent = new Intent(this, WebbVy.class);
intent.putExtra("LINK", mLink);
startActivity(intent);
}
});
然后在您的webview活动中,您将从意图中获取字符串并将其加载到webview中。
希望它有所帮助!