2011-05-24 65 views
4

我在這裏擊敗我的頭撞牆。我只是創建一個webview,它也有能力前進,刷新和返回。我已經玩了兩個星期的Android開發工具包,覺得我相當的遠,但是無論我做什麼,我似乎都無法解決這個問題。Android的webview用戶界面

我只是想按下菜單按鈕並獲得「返回」,「刷新」和「轉發」選項。到目前爲止,我已經設法讓按鈕出現在我的webview中,但是他們沒有執行任何操作,因爲沒有任何反應。對於新手來說,任何解決方案都是非常棒的。

我的代碼包含如下: XXXXX.java --------------------------

 

    package com.AFMoB.XXXXX; 

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

    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_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:focusableInTouchMode="true"> 
    </WebView> 
    <ProgressBar 
     android:id="@+id/myProgressBar" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:max="100" 
     android:progress="30" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 
    </LinearLayout> 

**manifest**------------------------ 

    <?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"> 
     <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> 

回答

1

的地方,你有評論說
「//當你點擊一個菜單項稱爲」
要調用onCreateOptionsMenu,
應該

 
public boolean onOptionsItemSelected(MenuItem item) { 
} 
+0

謝謝你的答覆。這工作完美。我也刪除了行'item.setIcon(R.drawable.back);'因爲我發現使用我自己的圖標是沒用的。我嘗試了一切努力讓我的'前進'和'後退'工作,但一直非常失敗。 刷新工作[我想通過改變方法來讀'webView.reload();'而不是'webView.refresh();' 任何人都可以協助前進和後退?我嘗試過'前進','canGoForward'和'goForward'以及'back','canGoBack'和'goBack'。沒有錯誤代碼,但是這些方法都沒有實際做任何事情。 – 2011-05-31 22:22:32

+0

如果答案已解決您的問題,請檢查答案是否正確。對於其他事情,作爲一個單獨的問題提問,那些對此有所瞭解的人將能夠幫助你。 – sat 2011-06-01 02:29:20

+0

再次感謝您的幫助,我確實得到了前進和回到工作! – 2011-06-02 23:01:58

1

應該

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

,而不是

case 1: 
webView.canGoBack();