Android UI 使用HTML布局(直接打开服务器网页)
2015-02-27 10:45
801 查看
很多时候我们用HTML布局会更方便直接,记录一下。
我现在主要是直接调用服务器的网页(实际上是jsp的,只是返回的是html),所以需要联网,第一步添加权限。
[html] view
plaincopy
<uses-permission android:name="android.permission.INTERNET" />
布局文件直接用一个WebView,如下:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
下面就可以直接写代码了:
[java] view
plaincopy
package com.yangshidesign.testgryoscope;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;
public class AddEmojiActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_addemoji);
webView = (WebView) this.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebPlugin(), "WebPlugin");
webView.loadUrl(this.getString(R.string.server_url));
}
/**
* 插件类,在html的js里面直接调用
*/
private class WebPlugin {
@JavascriptInterface
public void test() {
Log.e("miquan", "kkkkkk");
Toast.makeText(AddEmojiActivity.this, "test toast ", Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public String test2() {
return "something";
}
}
}
其中@JavascriptInterface注解是添加在每一个需要用到的方法上面的。
最后就可以直接在HTML网页上调用了。
[html] view
plaincopy
<script type="text/javascript">
function test() {
WebPlugin.test();
var something = WebPlugin.test2();
}
</script>
我现在主要是直接调用服务器的网页(实际上是jsp的,只是返回的是html),所以需要联网,第一步添加权限。
[html] view
plaincopy
<uses-permission android:name="android.permission.INTERNET" />
布局文件直接用一个WebView,如下:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
下面就可以直接写代码了:
[java] view
plaincopy
package com.yangshidesign.testgryoscope;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;
public class AddEmojiActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_addemoji);
webView = (WebView) this.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebPlugin(), "WebPlugin");
webView.loadUrl(this.getString(R.string.server_url));
}
/**
* 插件类,在html的js里面直接调用
*/
private class WebPlugin {
@JavascriptInterface
public void test() {
Log.e("miquan", "kkkkkk");
Toast.makeText(AddEmojiActivity.this, "test toast ", Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public String test2() {
return "something";
}
}
}
其中@JavascriptInterface注解是添加在每一个需要用到的方法上面的。
最后就可以直接在HTML网页上调用了。
[html] view
plaincopy
<script type="text/javascript">
function test() {
WebPlugin.test();
var something = WebPlugin.test2();
}
</script>
相关文章推荐
- Android UI 使用HTML布局(直接打开服务器网页)
- Android UI 使用HTML布局(直接打开服务器网页)
- Android UI 使用HTML布局(直接打开server网页)
- Android UI使用HTML布局方法实例
- android 中直接在布局文件中使用html 设置不同的颜色
- Android向右滑动删除布局-直接使用方法说明
- Android开发_WebView组件使用详解_LoadUrl直接显示网页内容
- 让360浏览器使用特定的模式打开网页(HTML)
- 如何在android中使用html作布局文件
- Android UI设计之<六>使用HTML标签,实现在TextView中对部分文字进行加粗显示
- 2011/06/13 android使用html做UI的方法---js与java的相互调用
- 使用android内置Intent打开网页、拨号、打电话
- Android 中使用 html 作布局文件
- Android在网页下(WebView中运用HTML)点击按钮打开本地手机应用程序
- 2011/06/13 android使用html做UI的方法---js与java的相互调用
- android UI进阶之布局的优化---使用include和merge
- Android使用WebView打开网页的实例
- Android 中使用 html 作布局文件
- android使用主流浏览器打开网页,无需弹出选择。
- android使用webView打开本地html文件