是否有更好的方法来刷新WebView?

cdg*_*cdg 44 android refresh reload webview

好.我一直在寻找,而我的小脑子却无法理解更新活动的更好方法.我能理解的任何建议都会很棒.:)

这是java代码:

package com.dge.dges;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class dgeActivity extends Activity {
 WebView mWebView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings();
        mWebView.loadUrl("http://www.websitehere.php");

        Button newButton = (Button)findViewById(R.id.new_button);
        newButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    Intent intent = new Intent(dgeActivity.this, dgeActivity.class);
    startActivity(intent);
   }
        });

    }
}
Run Code Online (Sandbox Code Playgroud)

这是main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/RelativeLayout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#000000"

>

 <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:scrollbars="none"

 />

 <Button
  android:id="@+id/new_button"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:text="Refresh"
 />





</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我不喜欢在活动后重新堆叠活动的想法.必须有一种更简单的方法来刷新webview.请帮忙.:)

小智 82

你可以称之为mWebView.reload();它的功能

  • 如果原始查询是WebView.postUrl(),reload()似乎不起作用 (5认同)

Pen*_*m10 62

1)如果您想重新加载相同的URL:

mWebView.loadUrl("http://www.websitehere.php");
Run Code Online (Sandbox Code Playgroud)

所以完整的代码将是

newButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    dgeActivity.this.mWebView.loadUrl("http://www.websitehere.php");
   }});
Run Code Online (Sandbox Code Playgroud)

2)mWebView.reload()如果请求是POST,你也可以打电话但要知道这会重新发布一个页面,所以只能在GET上正常工作.

  • 你太摇滚了!非常感谢.它工作得很好!WHOO HOOOO !!!! :) (5认同)

dra*_*oot 44

最好的解决方案就是做webView.loadUrl( "javascript:window.location.reload( true )" );.这应适用于所有版本,不会引入新的历史记录条目.

  • 好的解决方案 但是,这需要您使用`yourWebView.getSettings()在WebView小部件中启用JavaScript.setJavaScriptEnabled(true);` (5认同)

Tho*_*401 11

这对我有用.

刚刚放入onCreate metode

Button button = (Button) findViewById(R.id.reload_btn);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            web.loadUrl( "javascript:window.location.reload( true )" );             
        }
    });
Run Code Online (Sandbox Code Playgroud)

这是可以在webView中重新加载网站的代码

web.loadUrl( "javascript:window.location.reload( true )" );  
Run Code Online (Sandbox Code Playgroud)


Oub*_*aan 5

尝试这个 :

mWebView.loadUrl(mWebView.getUrl().toString());
Run Code Online (Sandbox Code Playgroud)

  • 或`mWebView.reload()`。 (5认同)