我是靠谱客的博主 负责芝麻,最近开发中收集的这篇文章主要介绍安卓 html event,android - Injecting JavaScript onclick() event inside WebView - Stack Overflow,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Finally I found the answer...

webView.loadUrl("javascript:(function(){document.getElementById('buttonClick').click();})()");

here is the complete source code

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.webkit.WebChromeClient;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class Huddle extends Activity {

private WebView wv;

EditText editText;

TextView textView;

Button button,buttonClick;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

editText = (EditText)findViewById(R.id.titlebar);

textView = (TextView)findViewById(R.id.txt_html);

button = (Button)findViewById(R.id.button);

buttonClick = (Button)findViewById(R.id.buttonClick);

wv = (WebView) findViewById(R.id.webview);

wv.getSettings().setJavaScriptEnabled(true);

wv.getSettings().setUserAgentString("Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

wv.setWebChromeClient(new MyChromeClient());

wv.loadUrl("file:///android_asset/www/index.html");

wv.requestFocus();

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String txt = editText.getText().toString();

wv.loadUrl("javascript:sayHelloFromJS('"+txt+"')");

}

});

buttonClick.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

wv.loadUrl("javascript:(function(){document.getElementById('buttonClick').click();})()");

}

});

}

class MyJavaScriptInterface

{

@SuppressWarnings("unused")

public void showHTML(final String html)

{

runOnUiThread(new Runnable() {

@Override

public void run() {

Huddle.this.textView.setText(html);

}

});

}

}

class MyChromeClient extends WebChromeClient{

}

class MyWebClient extends WebViewClient {

@Override

public void onPageFinished(WebView view, String url) {

super.onPageFinished(view, url);

}

}

}

index.html

add this index.html to assetswwwindex.html

function sayHelloFromJS(value) {

document.getElementById("namefromAndroid").value = value;

}

function setHtml(){

var HTML = ""+document.getElementById("namefromjs").value;

window.HTMLOUT.showHTML(HTML);

}

function doAlert() {

alert("JavaScript says: Hello Android !!! How are you?");

}

Enter your name here

Name from Android is

Button Click

最后

以上就是负责芝麻为你收集整理的安卓 html event,android - Injecting JavaScript onclick() event inside WebView - Stack Overflow的全部内容,希望文章能够帮你解决安卓 html event,android - Injecting JavaScript onclick() event inside WebView - Stack Overflow所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部