webView簡單使用:網頁中有電話,在客戶端點選打電話(一)
阿新 • • 發佈:2019-02-12
一:
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>