【问题标题】:( Android ) Webview refresh button( Android ) Webview 刷新按钮
【发布时间】:2015-12-20 17:21:03
【问题描述】:

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

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

}

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>

感谢您的帮助

【问题讨论】:

标签: java android webview reload


【解决方案1】:

在活动类中:

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();
                
            }
        });
    }

}

在布局 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>

【讨论】:

  • Android Studio 在构建时显示“错误:(29, 39) 错误:找不到符号类 OnClickListener”
  • 导入android.view.View.OnClickListener;活动中
  • 我的荣幸:)。请投票回答,以便对其他人有所帮助
【解决方案2】:

有很多方法可以重新加载你的 webview:

  1. 你可以在任何方法/动作事件中使用webview.loadUrl( "javascript:window.location.reload( true )" );

  2. webview.loadUrl(webview.toString); //getting the webview URL and use to reload ur web page

  3. webview.loadUrl(url); // you can use your desire URL to reload your web page

  4. webview.loadUrl( "onclick=\"window.history.back()"\");

【讨论】:

    【解决方案3】:

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

    yourActivity.this.webView.loadUrl(url);
    

    点击按钮。

    【讨论】:

      猜你喜欢
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      相关资源
      最近更新 更多