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://www.uj5u.com/shujuku/213648.html
標籤:其他
下一篇:MySQL-8.0.20
