android studio嵌入HTML5,html - Android Studio - How to load HTML5 content from a local path using Webv...

这篇博客详细介绍了如何在Android活动中调用WebView内的JavaScript代码,包括显示Toast、打开对话框以及从活动向JavaScript传递消息的功能。通过设置JavaScriptInterface和启用JavaScript,实现了Android与网页的双向通信。
摘要由CSDN通过智能技术生成

Call JavaScript inside WebView from Android activity.

It is very good!

/assets/mypage.html

My HTML

MyHTML

Hello!

function showAndroidToast(toast) {

AndroidFunction.showToast(toast);

}

function openAndroidDialog() {

AndroidFunction.openAndroidDialog();

}

function callFromActivity(msg){

document.getElementById("mytext").innerHTML = msg;

}

main.xml

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:id="@+id/msg"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

android:id="@+id/sendmsg"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Msg to JavaScript"

/>

android:id="@+id/mybrowser"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

/>

main code:

import android.app.Activity;

import android.app.AlertDialog;

import android.content.Context;

import android.os.Bundle;

import android.view.View;

import android.webkit.WebView;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class AndroidHTMLActivity extends Activity {

WebView myBrowser;

EditText Msg;

Button btnSendMsg;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myBrowser = (WebView)findViewById(R.id.mybrowser);

final MyJavaScriptInterface myJavaScriptInterface

= new MyJavaScriptInterface(this);

myBrowser.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");

myBrowser.getSettings().setJavaScriptEnabled(true);

myBrowser.loadUrl("file:///android_asset/mypage.html");

Msg = (EditText)findViewById(R.id.msg);

btnSendMsg = (Button)findViewById(R.id.sendmsg);

btnSendMsg.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

String msgToSend = Msg.getText().toString();

myBrowser.loadUrl("javascript:callFromActivity(\""+msgToSend+"\")");

}});

}

public class MyJavaScriptInterface {

Context mContext;

MyJavaScriptInterface(Context c) {

mContext = c;

}

public void showToast(String toast){

Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();

}

public void openAndroidDialog(){

AlertDialog.Builder myDialog

= new AlertDialog.Builder(AndroidHTMLActivity.this);

myDialog.setTitle("DANGER!");

myDialog.setMessage("You can do what you want!");

myDialog.setPositiveButton("ON", null);

myDialog.show();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值