java (Android) Webview 刷新按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32738756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:41:39  来源:igfitidea点击:

( Android ) Webview refresh button

javaandroidwebviewreload

提问by k0spwn

I'm trying to create a button to reload a Webview but ( sorry I'm new in android developement ) I didn't find a way to do it ( even in stackoverflow )

我正在尝试创建一个按钮来重新加载 Webview,但是(对不起,我是 android 开发的新手)我没有找到方法(即使在 stackoverflow 中)

Here is the Mainactivity.java

这是 Mainactivity.java

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;

import static com.d4rkunicorn.partyhard.R.id.webView;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webview = new WebView(this);
        setContentView(webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("url");
    }

}

The webview.xml :

webview.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:id="@+id/webview.xml">

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="REFRESH"
        android:id="@+id/button" />

 </RelativeLayout>

Thanks for helping

谢谢你的帮助

回答by Android Team

In Activity class :

在活动类中:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;

import static com.d4rkunicorn.partyhard.R.id.webView;


public class MainActivity extends ActionBarActivity {

     WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview = new WebView(this);
        setContentView(webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("url");

        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                webview.reload();

            }
        });
    }

}

In Layout XML :

在布局 XML 中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:id="@+id/webview.xml">

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

    <Button
         android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="REFRESH"
        android:id="@+id/button" />

 </RelativeLayout>

Hope it will help you !!

希望能帮到你!!

回答by Michael

There many possible way to reload your webview, one the method is 1. you can use webview.loadUrl( "javascript:window.location.reload( true )" );inside any method/action event 2. webview.loadUrl(webview.toString); //geting the webview url and use to reload ur web page3. webview.loadUrl(url); // you can use your desire url to reload ur web page4. webview.loadUrl( "onclick=\"window.history.back()"\");

有许多可能的方式来重新加载的网页视图,一个方法是1,你可以用 webview.loadUrl( "javascript:window.location.reload( true )" );任何方法/行动事件2中webview.loadUrl(webview.toString); //geting the webview url and use to reload ur web page3 webview.loadUrl(url); // you can use your desire url to reload ur web page4。webview.loadUrl( "onclick=\"window.history.back()"\");

回答by KishuDroid

You can reload your web view or you can use below code :

您可以重新加载 Web 视图,也可以使用以下代码:

yourActivity.this.webView.loadUrl(url);

on your button click.

在您的按钮上单击。