您的位置:首页 > 移动开发

webView简单使用:网页中有电话,在客户端点击打电话(一)

2016-05-23 01:19 435 查看
一:

layout 中的布局文件 activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</RelativeLayout>


二:在MainActivity 的代码

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
//webView相当于浏览器  声明webView
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化webview 控件
webView=(WebView) findViewById(R.id.webView);
//需要显示在移动客户端的网页地址 URL
webView.loadUrl("http://172.00.00.212:8080/manager/myhtml/mall.html");
//不加,单击超连接,启动系统的浏览器,加了之后在我们自己的APP中显示网页。
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading
(WebView view, String url) {
Log.i("用户单击超连接", url);
//判断用户单击的是那个超连接
String tag="tada:tel";

if (url.contains(tag))
{
String mobile=url.substring(url.lastIndexOf("/")+1);
Uri uri=Uri.parse("tel:"+mobile);
Intent intent=new Intent(Intent.ACTION_CALL,uri);
startActivity(intent);
//这个超连接,java已经处理了,webview不要处理了
return true;
}

return super.shouldOverrideUrlLoading(view, url);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


三:提前做好的要显示的网页

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title></title>
</head>

<body>
<table bgcolor="greenyellow" border="0" width="100%" height="100">
<tr>
<td colspan="3" align="center">
<h1>商城</h1></td>
</tr>
<tr>
<td>首页</td>
<td>商品</td>
<td><a href="index.html">订单</a> </td>
</tr>
</table>

<table bgcolor="gray" width="100%">
<tr>
<td bgcolor="aqua" width="100">
<table>
<tr>
<td>分类1</td>
</tr>
<tr>
<td>分类2</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td><img src="img/11.jpg" width="200"></td>
</tr>
<tr>
<td><img src="img/12.jpg" width="200"></td>
</tr>
</table>

</td>
</tr>
</table>

<table bgcolor="greenyellow" width="100%" height="60">
<tr><a href="tada:tel/13698888">联系电话:一小时送货上门</a>
<a href="tarena:writedb/1#java&2#anaroid">保存</a>
</tr>
</table>
</body>

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