首页 > 代码库 > 应用开始界面简单倒计时的dialog

应用开始界面简单倒计时的dialog

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF85C17E"
    tools:context="com.example.lesson7_2_id19_dialog.MainActivity">
</RelativeLayout>

dialog_start.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="@mipmap/timg">
 6 
 7     <TextView
 8         android:id="@+id/tv_time"
 9         android:gravity="center"
10         android:background="@drawable/oval"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="3s"
14         android:layout_alignParentRight="true"
15         android:layout_margin="20dp"/>
16 
17 </RelativeLayout>

drawable下oval.xml  自定义圆圈

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:shape="oval">
 4     <padding
 5         android:bottom="5dp"
 6         android:left="5dp"
 7         android:right="5dp"
 8         android:top="5dp" />
 9     <stroke
10         android:width="1dp"
11         android:color="#7bf0f2" />
12 </shape>

java代码:

 1 package com.example.lesson7_2_id19_dialog;
 2 
 3 import android.app.Dialog;
 4 import android.content.Context;
 5 import android.os.Bundle;
 6 import android.os.CountDownTimer;
 7 import android.support.annotation.NonNull;
 8 import android.widget.TextView;
 9 
10 /**
11  * Created by Administrator on 2017/3/8 0008.
12  */
13 
14 public class StartDialog extends Dialog {
15     // 继承dialog重写构造方法
16     public StartDialog(@NonNull Context context) {
17         super(context, R.style.DialogStyle);
18 
19     }
20 
21     TextView tv_time;
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.dialog_start);
27         // 设置是否可以关闭当前控件
28         setCancelable(false);
29         // 找到tv_time控件
30         tv_time = (TextView) findViewById(R.id.tv_time);
31         new DownTimer().start();
32     }
33 
34         // 继承CountDownTimer类
35     class DownTimer extends CountDownTimer {
36 
37         public DownTimer() {
38             // 设置时间4秒
39             super(4000, 1000);
40         }
41         // 重写CountDownTimer的两个方法
42         @Override
43         public void onTick(long millisUntilFinished) {
44             tv_time.setText(millisUntilFinished / 1000 + "s");
45         }
46 
47         @Override
48         public void onFinish() {
49             StartDialog.this.dismiss();
50 
51         }
52 
53     }
54 }
 1 package com.example.lesson7_2_id19_dialog;
 2 
 3 import android.content.DialogInterface;
 4 import android.os.Bundle;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.widget.Toast;
 7 
 8 public class MainActivity extends AppCompatActivity {
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14         StartDialog dialog = new StartDialog(this);
15         dialog.show();
16         dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
17             @Override
18             public void onDismiss(DialogInterface dialog) {
19                 Toast.makeText(MainActivity.this, "应用开始页面关闭", Toast.LENGTH_SHORT).show();
20             }
21         });
22     }
23 }

 

应用开始界面简单倒计时的dialog