用户界面 - 安卓网络视图 UI



我在这里用头撞墙。我只是在创建一个WebView,它也具有前进、刷新和后退的能力。我已经玩了两个星期的Android开发工具包,感觉我相当远,但是无论我做什么,我似乎都无法解决这个问题。

我只想按下菜单按钮并获得"后退","刷新"和"前进"选项。到目前为止,我已经设法让按钮出现在我的WebView但是它们不执行任何操作,因为什么也没发生。对于[到处搜索]的新手来说,任何解决方案都会很棒。

我的代码包含在下面: XXXXX.java

package com.AFMoB.XXXXX;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class XXXXX extends Activity { //** Called when the activity is first created. */
    public WebView webView;    //DECLARE webview variable outside of onCreate function so we can access it in other functions (menu)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // Part of the progress bar system for future   use-->   this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        webView = (WebView) findViewById(R.id.webview); // Create an instance of WebView and set it to the layout component created with id webview in main.xml
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true); // Enables Java
        webView.setWebViewClient(new WebViewClient()); // Opens web links clicked by user in the webview
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) { // Handle the error
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.loadUrl("http://www.afdc.energy.gov/afdc/locator/m/stations/");
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu); // Add menu items, second value is the id, use this in the onCreateOptionsMenu
        menu.add(0, 1, 0, "Back");
        menu.add(0, 2, 0, "Refresh");
        menu.add(0, 3, 0, "Forward");
        return true; // End of menu configuration
    }
    public boolean onCreateOptionsMenu(MenuItem item) { // Called when you tap a menu item
        switch (item.getItemId()) {
            case 1: //If the ID equals 1 , go back
                webView.canGoBack();
                item.setIcon(R.drawable.back); // Occurs after tapping the back menu item
                return true;
            case 2: //If the ID equals 2 , go refresh
                webView.reload();
                item.setIcon(R.drawable.refresh);
                return true;
            case 3: //If the ID equals 3 , go forward
                webView.canGoForward();
                item.setIcon(R.drawable.forward);
                return true;
        }
        return false;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) { // Enables browsing to previous pages with the hardware back button
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // Check if the key event was the BACK key and if there's history
            webView.goBack();
            return true;
        }    // If it wasn't the BACK key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <WebView
        android:id="@+id/webview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true"></WebView>
    <ProgressBar
        android:id="@+id/myProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" />
</LinearLayout>

安卓清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.AFMoB.XXXXX"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">
        <activity
            android:name=".XXXXX"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

您评论
过的地方 "//点击菜单项时调用"
您正在调用创建选项菜单,
它应该是

public boolean onOptionsItemSelected(MenuItem item) {}

它应该是

case 1:
if (webView.canGoBack())
webView.goBack();

而不是

case 1:
webView.canGoBack();

相关内容

  • 没有找到相关文章

最新更新