0

我将添加什么代码,以便在按下菜单按钮刷新时它会刷新我的应用程序中的 webview。?

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;


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

    WebView WV = (WebView) findViewById(R.id.webView1);
    WV.getSettings().setJavaScriptEnabled(true);
    WV.loadUrl("http://home.btconnect.com/MrJFisher/");



}

@Override
public boolean onCreateOptionsMenu (Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.item1){
        WebView.reload();
    }
    return super.onOptionsItemSelected(item);
}
};

初学者。所以我道歉。

4

2 回答 2

2

编辑

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;

public class Main 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.webView1);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://home.btconnect.com/MrJFisher/");
    }

    @Override
    public boolean onCreateOptionsMenu (Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }  

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.item1){
            mWebView.reload();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
于 2012-07-19T15:17:47.210 回答
1
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId()== R.id.item1){
        WV.reload(); 
        return true;
    }
    return super.onOptionsItemSelected(item);
}

请参阅WebView 文档

于 2012-07-19T15:18:40.107 回答