0

我在本地HTML頁面中顯示圖像,並有一個用於單擊事件的按鈕。當點擊這個按鈕時,我需要調用另一個活動。它正在工作。但是,問題出在另一個後,回到HTML頁面的圖像不顯示和應用程序不工作。Android HTML按鈕onclick事件

<script language="javascript"> 
function validClick() { 
    valid.performClick(); 
    document.getElementById("ok").value = "start"; 
} 
function refuseClick(){ 
    refuse.performClick(); 
    document.getElementById("no").value = "Je refuse"; 
} 

<div> 
<button type="button" id="ok" 
    style="font-weight: 700; margin-right: 20px;" onclick="validClick();">Start</button> 

</div> 

HTML頁面包含圖像:

<li><a class="box" href="products.html" data-transition="fade" > <img src="img/products.png" alt="Products"> <span class="text"> Products</span> 

MainActivity:

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.about_screen); 


     WebView webview = (WebView) findViewById(R.id.webview); 
     webview.loadUrl("file:///android_asset/index.html"); 

     valid = new Button(this); 
     valid.setOnClickListener(this); 
     refuse = new Button(this); 
     refuse.setOnClickListener(this); 

    // Enablejavascript 
     WebSettings ws = webview.getSettings(); 
     ws.setJavaScriptEnabled(true); 
     // Add the interface to record javascript events 
     webview.addJavascriptInterface(valid, "valid"); 
     webview.addJavascriptInterface(refuse, "refuse"); 

} 


    @Override 
    public void onClick(View v) { 
     if (v.equals(valid)) { 

     Intent i = new Intent(this, CloudReco.class); 
     startActivity(i); 
     } else if (v.equals(refuse)) { 
      //do Something else } 
    } 
+0

您能澄清圖像不顯示,應用程序無法正常工作嗎?您列出的HTML中沒有圖像。 – ksasq

+0

你可以檢查編輯的代碼 – user3351727

+0

我仍然不能確切地告訴你應用程序的意思是不工作,但我已經發布了一些提示,應該會改善你的程序。如果您仍然遇到問題,請發佈問題的確切詳細信息; 「它不工作」太模糊了! :-) – ksasq

回答

2

有你需要檢查你的代碼幾件事情。

首先,在您的HTML中,頂部的<script>標記未關閉。請在上次函數定義後添加</script>

其次,在Java中,請在調用loadUrl之前配置您的WebView。設置和JavaScript界面​​應首先註冊。

第三,不需要使用Button小部件來實現您正在尋找的效果。類似這樣的應該是完全足夠的:

webview.addJavaScriptInterface(new Object() { 
    @JavaScriptInterface 
    public void performClick() { 
     Intent i = new Intent(this, CloudReco.class); 
     startActivity(i); 
    } 
}, "valid"); 

和類似的「拒絕」。

0

我用來傳遞大量的JavaScript事件的下一個「奇怪」的方法(最後一次使用是來回滾動事件,我也通過串接值):

webView.setWebViewClient(new WebViewClient() { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (url.contains("button1Event")) { 
      // button1 was clicked inside webview html 
      Toast.makeText(context, 'Button1WasClicked', Toast.LENGTH_SHORT); 
      return true; (to avoid a default redirect to the url) 
     } else if (url.contains("button2Event")) { 
      // button2 was clicked inside webview html 
      Toast.makeText(context, 'Button2WasClicked', Toast.LENGTH_SHORT); 
      return true; (to avoid a default redirect to the url) 
     } 
     return false; 
     } 
} 

HTML包含虛擬<a>元素用於模擬URLclick其中URL是我們在本地代碼中等待的一個KEY:

<!DOCTYPE html> 
<html> 
<body> 
<a id='yourLinkID'></a> 

<button onclick="myFunction('button1Event')">Button1</button> 
<button onclick="myFunction('button2Event')">Button2</button> 

<script> 
function myFunction(value) { 
    document.getElementById('yourLinkID').href = 'testtest' + value; 
    document.getElementById('yourLinkID').click(); 
// we set the wanted URL and simulate the click 
} 
</script> 

</body> 
</html>