2011-07-11 65 views
0

我在if語句中使用var「str」時遇到了問題。我知道這是一個範圍問題,但我不知道如何解決它。我失敗了好幾次。onKeyDown事件的作用域變量問題

我的目標是利用「海峽」的值在if語句中顯示警報與否。 限制是我必須以這種方式分配「str」的值。

public class MyJavaScriptInterface 
{ 
    public String showHTML(String html) 
    { 
     String str = html; 
     return str; 
    } 
} 



    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 

     //Handle the back button 
     if(keyCode == KeyEvent.KEYCODE_BACK && str =="0") 
     { 
      //Ask the user if they want to quit 

更新:

OK我是儘量不與閱讀整個代碼打擾你,但它的短,可能有幫助。

在網頁視圖顯示我的html頁面,我也讀了隱藏變量將決定是否一個警告框應該顯示與否。我只是想在keydown推送中的if語句中獲得該值。這是整個代碼。

package com.ishop.pizzaoven; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class buttonOne extends Activity 
{ 
WebView wb = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.buttons); 
    wb = new WebView(this); 
    wb.getSettings().setJavaScriptEnabled(true); 
    wb.setWebViewClient(new HelloWebViewClient()); 
    wb.getSettings().setJavaScriptEnabled(true); 
    wb.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT"); 
    /* WebViewClient must be set BEFORE calling loadUrl! */ 
    wb.setWebViewClient(new WebViewClient() 
    { 
     @Override 
     public void onPageFinished(WebView view, String url) 
     { 
      /* This call inject JavaScript into the page which just finished loading. */ 
      wb.loadUrl("javascript:window.HTMLOUT.showHTML(document.getElementById('sendtextcoupon').value);"); 
     } 
    }); 
    wb.loadUrl("http://ishopstark.com/mobileapp.php?category=1"); 
    setContentView(wb); 
} 

private class HelloWebViewClient extends WebViewClient 
{ 
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    { 
     view.loadUrl(url); 
     return true; 
    } 
} 

final Context myApp = this; 

public class MyJavaScriptInterface 
{ 
    public void showHTML(String html) 
    { 
    String str = html; 
} 
} 




    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 

     //Handle the back button 
     if(keyCode == KeyEvent.KEYCODE_BACK) 
     { 
      //Ask the user if they want to quit 
      new AlertDialog.Builder(this) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .setTitle("Coupon") 
      .setMessage("Do you want a coupon texted to you?") 
      .setPositiveButton("YES!", new DialogInterface.OnClickListener() 
      { 
       @Override 
       public void onClick(DialogInterface dialog, int which) 
       { 
        //Stop the activity 
        Toast.makeText(buttonOne.this, "Great! You'll get one in just a couple of minutes.", Toast.LENGTH_LONG).show(); 
        finish(); 
       } 
      }) 
      .setNegativeButton("Not now", new DialogInterface.OnClickListener() 
      { 
       @Override 
       public void onClick(DialogInterface dialog, int which) 
       { 
        //Stop the activity 
        finish(); 
       } 
      }) 
      .show();   
      return true; 
     } 
     else 
     { 
      return super.onKeyDown(keyCode, event); 
     } 

    } 

}// End of main class 

更新2:

行,所以我改變了我的代碼位。但是,現在我收到警報錯誤的任何想法? 「該方法的setIcon(INT)是未定義的類型buttonOne.MyJavaScriptInterface」我得到.setIcon(android.R.drawable.ic_dialog_alert)這個錯誤並返回super.onKeyDown(的keyCode,事件);

public class MyJavaScriptInterface 
{ 
    public String showHTML(String html) 
    { 
    String str = html; 
    return str; 
    } 
    public boolean onKeyDown(int keyCode, KeyEvent event, String str) 
    { 

     //Handle the back button 
     if(keyCode == KeyEvent.KEYCODE_BACK && str == "0") 
     { 
      //Ask the user if they want to quit 
      AlertDialog.Builder alertbox = new  AlertDialog.Builder(buttonOne.this); 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .setTitle("Coupon") 
      .setMessage("Do you want a coupon texted to you?") 
      .setPositiveButton("YES!", new DialogInterface.OnClickListener() 
      { 
       @Override 
       public void onClick(DialogInterface dialog, int which) 
       { 
        //Stop the activity 
        Toast.makeText(buttonOne.this, "Great! You'll get one in just a couple of minutes.", Toast.LENGTH_LONG).show(); 
        finish(); 
       } 
      }) 
      .setNegativeButton("Not now", new DialogInterface.OnClickListener() 
      { 
       @Override 
       public void onClick(DialogInterface dialog, int which) 
       { 
        //Stop the activity 
        finish(); 
       } 
      }) 
      .show();   
      return true; 
     } 
     else 
     { 
      return super.onKeyDown(keyCode, event); 
     } 

    } 
} 
+0

隨着答案@Steve,我會說使用String的**等於**方法,而不是** == **。 –

+0

onKeyDown事件處理函數是什麼類? –

回答

2

不確定你的目標是什麼,但你不能讓字符串變成一個成員變量嗎?

public class MyJavaScriptInterface 
{ 
private String str; 
public String showHTML(String html) 
{ 
    str = html; 
    return str; 
    } 
} 
+0

我的目標是在讀取if(keyCode == KeyEvent.KEYCODE_BACK && str ==「0」)的方法中使用我的方法public String showHTML(String html)中的「str」。看到str ==「0」?那就是我需要的 – Rick

+0

史蒂夫提供的代碼將完成這一工作。 –

+0

好,所以我改了一下我的代碼。但是,現在我收到警報錯誤的任何想法?代碼不會適合在這裏把它放在一個答案 – Rick

1

這將有助於瞭解偵聽器方法在哪個類中;你有它在MyJavaScriptInterface類之外,但沒有任何關於它的上下文。所以當onKeyDown發生時,你是否有html來運行showHTML(html)方法?它已經運行?你有一個MyJavaScriptInterface的實例來運行它嗎?

如果你有一個額外}在那裏,它確實是同一類的一部分,那麼我會@同意史蒂夫的答案。

如果它是類外,再就是根據你的實際情況,許多解決方案。最簡單的方法是使海峽的實例變量作爲@Steve說,然後添加一個訪問方法:

public String getStr(){ 
    return str; 
} 

,然後讓聽衆調用訪問,而不是試圖使用的STR直接:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    // Get an instance of MyJavaScriptInterface; 
    MyJavaScriptInterface mjsi = new MyJavaScriptInterface(); 

    //Handle the back button 
    if(keyCode == KeyEvent.KEYCODE_BACK && mjsi.getStr().equals("0")) 
    { 
     //Ask the user if they want to quit 
+0

謝謝你工作!******* \t公共類MyJavaScriptInterface \t { \t \t公共無效showHTML(字符串HTML) \t { \t \t STR = HTML; \t \t \t } \t \t公共字符串getStr() \t \t { \t \t \t返回STR; \t \t} \t} \t公共布爾的onkeydown(INT的keyCode,KeyEvent的事件) \t { \t \t MyJavaScriptInterface mjsi =新MyJavaScriptInterface(); \t \t //處理後退按鈕 \t \t if(keyCode == KeyEvent.KEYCODE_BACK && mjsi.getStr()。equals(「1」)) – Rick