package com.example.mouth.fragment;
import androID.os.Bundle;
import androID.support.annotation.NonNull;
import androID.support.annotation.Nullable;
import androID.support.v4.app.Fragment;
import androID.vIEw.LayoutInflater;
import androID.vIEw.VIEw;
import androID.vIEw.VIEwGroup;
import androID.webkit.JavaScriptInterface;
import androID.webkit.JsResult;
import androID.webkit.WebChromeClIEnt;
import androID.webkit.WebResourceRequest;
import androID.webkit.WebSettings;
import androID.webkit.WebVIEw;
import androID.webkit.WebVIEwClIEnt;
import androID.Widget.button;
import com.example.mouth.R;
public class WoDeFragment extends Fragment {
private VIEw vIEw;
private WebVIEw webvIEw;
private button button_wode;
@Nullable
@OverrIDe
public VIEw onCreateVIEw(@NonNull LayoutInflater inflater,@Nullable VIEwGroup container,@Nullable Bundle savedInstanceState) {
vIEw = VIEw.inflate(getActivity(),R.layout.wodefragment,null);
return vIEw;
}
@OverrIDe
public voID onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
webvIEw = vIEw.findVIEwByID(R.ID.webvIEw);
button_wode = vIEw.findVIEwByID(R.ID.button_wode);
button_wode.setonClickListener(new VIEw.OnClickListener() {
@OverrIDe
public voID onClick(VIEw v) {
getActivity().runOnUiThread(new Runnable() {
@OverrIDe
public voID run() {
webvIEw.loadUrl("JavaScript:changecolor(‘#DCDCDC‘)");
}
});
}
});
webvIEw.loadUrl("https://abnerming8.github.io/abnerming.HTML");
//获取设置管理器
WebSettings settings = webvIEw.getSettings();
//开启权限可以跟Js交互的权限
settings.setJavaScriptEnabled(true);
//Alertd窗需要我们开权限
settings.setJavaScriptCanopenwindowsautomatically(true);
//添加你和HTML5要交互的方法 参数1:是定义两边需要交互的方法 参数2:就是HTML那边的调用者
webvIEw.addJavaScriptInterface(new TestTwo(),"androID");
webvIEw.setWebVIEwClIEnt(new WebVIEwClIEnt() {
@OverrIDe
public boolean shouldOverrIDeUrlLoading(WebVIEw vIEw,WebResourceRequest request) {
vIEw.loadUrl("https://abnerming8.github.io/abnerming.HTML");
return super.shouldOverrIDeUrlLoading(vIEw,request);
}
});
webvIEw.setWebChromeClIEnt(new WebChromeClIEnt() {
@OverrIDe
public boolean onjsAlert(WebVIEw vIEw,String url,String message,JsResult result) {
return super.onjsAlert(vIEw,url,message,result);
}
});
}
private class TestTwo {
@JavaScriptInterface
public voID show() {
getActivity().runOnUiThread(new Runnable() {
@OverrIDe
public voID run() {
webvIEw.loadUrl("JavaScript:toast()");
}
});
}
}
}
总结
以上是内存溢出为你收集整理的webview简单互调全部内容,希望文章能够帮你解决webview简单互调所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)