Android WebView JavaScript交互
2014-05-08 19:35
405 查看
介绍一下,Android中Webview与JavaScript的交互,首先是在布局文件里添加webview控件:
[html] view
plaincopy
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
然后是在manifest里添加权限:
[html] view
plaincopy
<uses-permission android:name="and
要是webview能够与JavaScript交互,首先需要webview要启用JavaScript:
[html] view
plaincopy
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
然后创建JavaScript的接口:
[java] view
plaincopy
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();
}
}
给webview添加JavaScript接口:
[html] view
plaincopy
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
本地JavaScript文件:
[javascript] view
plaincopy
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
整个代码如下:
[java] view
plaincopy
public class MainActivity extends Activity {
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
ProcessWebString();
}
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();
}
}
private void ProcessWebString() {
// 加载 asset 文件
String tpl = getFromAssets("web_tpl.html");
myWebView.loadDataWithBaseURL(null, tpl, "text/html", "utf-8", null);
}
/*
* 获取html文件
*/
public String getFromAssets(String fileName) {
try {
InputStreamReader inputReader = new InputStreamReader(
getResources().getAssets().open(fileName));
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "";
String Result = "";
while ((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
运行效果:
转自:http://blog.csdn.net/phj_981805903/article/details/12573735
[html] view
plaincopy
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
然后是在manifest里添加权限:
[html] view
plaincopy
<uses-permission android:name="and
要是webview能够与JavaScript交互,首先需要webview要启用JavaScript:
[html] view
plaincopy
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
然后创建JavaScript的接口:
[java] view
plaincopy
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();
}
}
给webview添加JavaScript接口:
[html] view
plaincopy
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
本地JavaScript文件:
[javascript] view
plaincopy
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
整个代码如下:
[java] view
plaincopy
public class MainActivity extends Activity {
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
ProcessWebString();
}
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();
}
}
private void ProcessWebString() {
// 加载 asset 文件
String tpl = getFromAssets("web_tpl.html");
myWebView.loadDataWithBaseURL(null, tpl, "text/html", "utf-8", null);
}
/*
* 获取html文件
*/
public String getFromAssets(String fileName) {
try {
InputStreamReader inputReader = new InputStreamReader(
getResources().getAssets().open(fileName));
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "";
String Result = "";
while ((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
运行效果:
转自:http://blog.csdn.net/phj_981805903/article/details/12573735
相关文章推荐
- Android客户端WebView与JavaScript之间的交互
- Android中WebView与JavaScript交互的两种方式
- ANDROID WEBVIEW和JAVASCRIPT交互_JS调用ANDROID方法
- Android WebView与JavaScript的交互使用
- Android WebView与JavaScript交互操作(Demo)
- android中Webview与javascript的交互(互相调用)
- android 的webView加载h5,和h5的交互(java和JavaScript的交互)
- Android中WebView加载本地Html,与JavaScript与Android交互: 一
- Android WebView与Javascript交互
- Android WebView与JavaScript 交互
- Android中Webview与javascript的交互之间的互相调用
- Android混合开发之WebViewJavascriptBridge实现JS与java安全交互
- WebView之javascript与android交互基础加强
- android中Webview与javascript的交互(互相调用)
- android WebView实现java与javascript的交互
- Android总结之WebView与Javascript交互[转]
- Android中WebView载入本地HTML代码并实现Java与JavaScript交互
- Android中webview跟JAVASCRIPT中的交互
- 在android中实现webview与javascript之间的交互(转)
- Android中Webview与JavaScript的交互(互相调用)