实现启动app,首页倒计时跳转,也可点击按钮进行跳转

2019-05-15  本文已影响0人  穿越平行宇宙

text_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="18dp" />
    <solid android:color="#1e000000" />

</shape>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#e9e9e9"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/count_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_margin="5dp"
        android:background="@drawable/text_background"
        android:padding="10dp"
        android:text="跳转" />


</RelativeLayout>


MainActivity.java

package com.example.gl.countdown_text;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 跳转
     */
    private final static int COUNT = 1;
    private TextView countDown;
    private Timer timer;
    private Handler handler = new Handler() {
        int num = 5;

        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case COUNT:
                    countDown.setText(String.valueOf("跳转:" + num) + "s");
                    num--;
                    break;

                default:
                    break;
            }
        }

        ;
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        initView();
    }

    private void initView() {
        countDown = (TextView) findViewById(R.id.count_down);
        //点击按钮进行直接跳转
        countDown.setOnClickListener(this);


        // sehedule的第二个参数是第一次启动延时的时间,第三个是每隔多长时间执行一次
        timer = new Timer();
        final long end = System.currentTimeMillis() + 1000 * 5;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(COUNT);
            }
        }, 0, 1000);

        //这里的schedule的第二个参数意义是到了这个时间尽快运行run里面的方法
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                startActivity(new Intent(MainActivity.this, MyActivity.class));
                finish();
                timer.cancel();
            }
        }, new Date(end));

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.count_down:
                startActivity(new Intent(MainActivity.this, MyActivity.class));
                //将timer进行取消
                finish();
                timer.cancel();
                break;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读