0

当最终用户单击选项菜单中的“item1”时,我想在 mywebview 中加载一个 url

这是我的代码:

package com.exemple.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.app.AlertDialog;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://m.google.com");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.item1:
                //AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                //alertDialog.setTitle("Reset...");
                //alertDialog.setMessage("Are you sure?");
                //alertDialog.show();
                myWebView.loadUrl("http://m.google.com");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

谢谢 !

4

1 回答 1

2

只是改变

WebView myWebView = (WebView) findViewById(R.id.webview);

myWebView = (WebView) findViewById(R.id.webview);

并将以下内容添加为类变量:

WebView myWebView;

所以你的最终代码变成:

public class MainActivity extends Activity {

    WebView myWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://m.google.info"); //You may want to comment this out, as you said you wanted to load it only when the user clicked
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.item1:
                //AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                //alertDialog.setTitle("Reset...");
                //alertDialog.setMessage("Are you sure?");
                //alertDialog.show();
                myWebView.loadUrl("http://m.google.com");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
于 2012-09-23T19:05:57.833 回答