您的位置:首页 > 产品设计 > UI/UE

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: