我有一些网站的ListView。我想要的是,当用户点击列表视图时,这些网站显示在同一webview中,而不是开始新的活动。我怎么能这样做?
这是我的webview java:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Aftonbladet extends Activity {
private WebView myWebView;
/** Called when the activity is first created. */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { // Enables browsing to previous pages with the hardware back button
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",
true);
myWebView = (WebView) findViewById(R.id.mywebview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if(pd.isShowing() && pd!=null)
{
pd.dismiss();
}
}
});
myWebView.loadUrl("http://www.google.com");
}
}
答案 0 :(得分:1)
对于在webview中加载网站,您可以像这样使用:
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.linkedin.com");
webview.getSettings().setPluginState(PluginState.ON);
webview.getSettings().setAllowFileAccess(true);
webview.setWebViewClient(new MyWebViewClient());
并且您的MyWebViewClient
班级是:
private class MyWebViewClient extends WebViewClient {
@Override
//show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl (url);
return true;
}
}
希望有所帮助
答案 1 :(得分:0)
编辑:看起来你想要的是将网址从一个活动传递到另一个活动。要实现这一点,您可以在ListView活动中使用SharedPreferences或putExtras这样的意图:
Intent myIntent = new Intent(this, Aftonbladet.class);
Bundle b = new Bundle();
b.putString("url", theURL);
myIntent.putExtra(b);
startActivity(myIntent);
然后在您的WebView Activity上,您可以获得该值:
Bundle b = this.getIntent().getExtras();
String url = b.getString("url");