2017-08-09 6 views
1

こんにちは私はこの問題を解決できません。私はメソッドonSwipeBottom()の変数をshowToast()から読みたいと思います。私はOOPの初心者ですので、もしあなたが私を助けてください。WebView変数android

スワイプで別のページを開きます。

OpenNews.js

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_open_news); 


     WebView myWebView = (WebView) findViewById(webview); 
     WebSettings webSettings = myWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 

     WebView webView = (WebView) findViewById(webview); 
     webView.addJavascriptInterface(new WebAppInterface(this), "android"); 

     myWebView.loadUrl("http://www.exaple.com"); 
     if(k != ""){ 
     myWebView.loadUrl(k); 
     } 
} 

/*this is declared before but is no matter we can use even sapmle()*/ 
public void onSwipeBottom() { 
    //WANT READ HERE var VALUE 
    String k = var; 
    Intent intent = new Intent(this, OpenNews.class); 
    startActivity(intent); 
} 

public class WebAppInterface { 
    Context mContext; 

    /** Instantiate the interface and set the context */ 
    WebAppInterface(Context c) { 
     mContext = c; 
    } 

    /** Show a toast from the web page */ 
    @JavascriptInterface 
    public void showToast(String toast) { 
     //Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); 
     String var = toast; 
     Log.d("", var); //here i have log in console with address from html file, everything is ok and work propertly. 
    } 
} 
} 

HTML

<script type="text/javascript"> 
    function showAndroidToast(toast) { 
     android.showToast(toast); 
    } 

document.addEventListener('DOMContentLoaded', function() { 
    showAndroidToast('http://www.example2.com'); 
}, false); 
</script> 
+0

ポストあなたの** JS **コード、そしてどのようにあなたがそれを編集して、質問と一緒にコードCezaryGołaśポスト@ ** WebAppInterface()** –

+0

を呼んでいます。また、解決する必要のある問題について詳しく説明してください。 –

答えて

0

は申し訳ありませんが、とても簡単です。

public static String k = ""; 
public void nextURL(String newURL){ 
     k = newURL; 
    } 

public class WebAppInterface { 
    Context mContext; 
     WebAppInterface(Context c) { 
     mContext = c; 
     } 
     public void showToast (String toast) { 
      nextURL(toast); 
     } 
}