Fragment webView for Button(呼叫或拨号)[Android Studio]
Posted
技术标签:
【中文标题】Fragment webView for Button(呼叫或拨号)[Android Studio]【英文标题】:Fragment webView for Button (to call or dial a number) [Android Studio] 【发布时间】:2020-05-11 10:26:36 【问题描述】:我已成功将WebView
添加到片段中,以加载网站。但是,网站上的某些元素(例如按钮)链接到tel=
方案,导致按下时出现err_unknown_url_scheme
错误。我该如何支持?
这是我的代码:
fragment_notification.xml
android:layout_
android:layout_>
<WebView
android:layout_
android:id="@+id/webView3"
android:layout_>
</WebView>
NotificationsFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.alidmisli.gcsdriver.R;
public class NotificationsFragment extends Fragment
public NotificationsFragment()
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_notifications, container, false );
WebView webView= (WebView)v.findViewById(R.id.webView3);
webView.getSettings().setjavascriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://gcs-bn.com/sos.html/");
return v;
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alidmisli.gcsdriver">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
【问题讨论】:
【参考方案1】:这样设置webviewclient
webView.setWebViewClient(new CustomWebViewClient());
String TELPREFIX = "tel:"
class CustomWebViewClient extends WebViewClient
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
if(url.startsWith(TELPREFIX))
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
return super.shouldOverrideUrlLoading(view, url);
【讨论】:
以上是关于Fragment webView for Button(呼叫或拨号)[Android Studio]的主要内容,如果未能解决你的问题,请参考以下文章
Android中Fragment装载WebView 返回键设置问题
Android中Fragment装载WebView 返回键设置问题