Android WebView
程序员文章站
2022-06-19 09:42:04
public class MainActivity extends AppCompatActivity { private ProgressBar progress1; private WebView webview1; private TextView titleStr; private Boolean flag = false; @Override protected void onCreate(Bundle savedInstanceState) {...
public class MainActivity extends AppCompatActivity {
private ProgressBar progress1;
private WebView webview1;
private TextView titleStr;
private Boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
initView();
}
private void initView() {
// 初始化控件
progress1 = (ProgressBar) findViewById(R.id.progressbar1);
webview1 = (WebView) findViewById(R.id.webview1);
titleStr = (TextView) findViewById(R.id.title);
// 载入网页
webview1.getSettings().setJavaScriptEnabled(true);
webview1.loadUrl("file:///android_asset/www/index.html");
// 显示和隐藏进度条
webview1.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
progress1.setVisibility(View.GONE);
progress1.setProgress(newProgress);
}else {
progress1.setVisibility(View.VISIBLE);
progress1.setProgress(newProgress);
}
}
});
// 签到成功以及提示
webview1.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
if(webview1.getUrl().equals("file:///android_asset/www/detail.html")){
// 改变actionBar 标题内容
titleStr.setText("积分签到");
}
if(webview1.getUrl().equals("file:///android_asset/www/detail.html#p1")){
// flag用于记录是否签到过
if(!flag){
Toast.makeText(MainActivity.this, "签到有彩蛋,积分+100", Toast.LENGTH_SHORT).show();
flag = true;
}else {
Toast.makeText(MainActivity.this, "您已领取", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
detail.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>任务1</title>
<link type="text/css" rel="stylesheet" href="index.css">
<script type="text/javascript" src="flexible.min.js"></script>
</head>
<body>
<a href="#p1"><img src="ticket.png" class="detail-img"></a>
</body>
</html>
布局资源:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="30dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:text="这是Title"
android:gravity="center"
android:textSize="20sp"
android:textColor="@android:color/white"
android:layout_height="wrap_content"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progressbar1"
android:layout_width="match_parent"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_height="20dp"/>
<WebView
android:id="@+id/webview1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
本文地址:https://blog.csdn.net/weixin_44043817/article/details/109624723
推荐阅读
-
Android开发实现简单的观察者与被观察者示例
-
Android开发实现Launcher3应用列表修改透明背景的方法
-
android 中viewpager+fragment仿微信底部TAG完美渐变
-
Android中RecyclerView实现多级折叠列表效果(二)
-
Android 在viewPager中双指缩放图片双击缩放图片单指拖拽图片的实现思路
-
Android Studio 引入 aidl 文件的方法汇总
-
Android sdutio配置Zxing进行扫码功能的实现方法
-
详解Android Studio 3.0的新特性与适配
-
Android仿微信底部菜单栏效果
-
android随机生成圆形云标签效果