webview网页视图demo2
程序员文章站
2022-06-26 14:51:16
package com.example.helloworld;import android.annotation.SuppressLint;import android.net.Uri;import android.os.Build;import android.os.Bundle;import android.text.Editable;import android.view.View;import android.webkit.WebChromeClient;import androi...
public class TestWebView extends AppCompatActivity {
TextView tv;
WebView wv;
Button bt;
EditText et;
String urlPrefix="https://";
long exitTime;
WebSettings setting;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testwebview);
init1();
}
public void init1() {
tv = findViewById(R.id.webviewtv);
wv = findViewById(R.id.webviewwv);
bt=findViewById(R.id.webviewwb);
et=findViewById(R.id.webviewet);
setting=wv.getSettings();
setting.setUseWideViewPort(true);
setting.setLoadWithOverviewMode(true);
setting.setBuiltInZoomControls(true);
setting.setDisplayZoomControls(false);
setting.setSupportZoom(true);
setting.setJavaScriptEnabled(true);
wv.setWebChromeClient(new WebChromeClient(){
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
tv.setText(title);
}
});
wv.setWebViewClient(new WebViewClient(){
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = et.getText().toString();
openURL(urlPrefix+url);
}
});
}
public void openURL(String url){
wv.loadUrl(url);
}
@Override
public void onBackPressed() {
if(wv.canGoBack()){
wv.goBack();
}else{
if(System.currentTimeMillis()-exitTime>2000){
Toast.makeText(getApplicationContext(),"再按一次退出程序",Toast.LENGTH_SHORT).show();
exitTime=System.currentTimeMillis();
}else{
super.onBackPressed();
}
}
}
}
本文地址:https://blog.csdn.net/weixin_43292547/article/details/110672034
上一篇: 惠普HP DeskJet2600打印机怎么更换墨盒?
下一篇: 用太极拳讲分布式理论,真舒服!
推荐阅读