有没有更好的方法来刷新 WebView? [英] Is there a better way to refresh WebView?

查看:71
本文介绍了有没有更好的方法来刷新 WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.我已经四处看了看,我的小脑袋无法理解刷新活动的更好方法.任何我能理解的建议都会很棒.:)

Ok. I have looked EVERYWHERE and my little brain just can't understand a better way to refresh an activity. Any suggestions that I can understand would be great. :)

这里是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);
            }
        });
    }
}

这是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>

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

I don't like the idea of just re-stacking activity after activity. There has to be an easier way to refresh the webview. Please help. :)

推荐答案

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

1) In case you want to reload the same URL:

mWebView.loadUrl("http://www.websitehere.php");

所以完整的代码是

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

2) 您也可以调用 mWebView.reload() 但请注意,如果请求是 POST,则会重新发布页面,因此只能与 GET 一起使用.

2) You can also call mWebView.reload() but be aware this reposts a page if the request was POST, so only works correctly with GET.

这篇关于有没有更好的方法来刷新 WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆