Android WebView

   日期:2020-11-12     浏览:164    评论:0    
核心提示: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>
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服