欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android WebView

程序员文章站 2022-03-18 22:26: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开发