android 启动页

  • 第一次进入:启动页-->引导页-->主页
  • 第二次进入:启动页-->主页
  • 解决:利用SharedPreferences存储一个布尔值,加载页面时判断实现
  • 启动页3秒倒计时,主线程不能更新UI,利用handler加Timer实现

SplashActivity

public class SplashActivity extends BaseActivity {
    private Button btnSkip;//跳过按钮
    private int num=3;//3秒倒数
    private Timer timer = new Timer();//定时器
    boolean isFirst =true;//定义布尔值

    @Override
    protected int getView() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
        //隐藏状态栏和导航栏
        Common.hideBar(this);
        btnSkip = findViewById(R.id.btn_skip);
    }

    @Override
    protected void method() {
        checkIsFirst();
        setClick();
    }

    /*
     * 判断是否为第一次进入,如果是进入引导页,否则进入主界面
     * 用SharedPreferences存储isFrist的值
     * */
    private void checkIsFirst() {
        final SharedPreferences sf=getSharedPreferences("isFirst",0);
        isFirst=sf.getBoolean("isFirst",true);
        if (isFirst){
            //隐藏跳过按钮
            btnSkip.setVisibility(View.INVISIBLE);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    SharedPreferences.Editor editor=sf.edit();
                    editor.putBoolean("isFirst",false);
                    editor.commit();
                    startActivity(new Intent(SplashActivity.this,GuideActivity.class));
                    SplashActivity.this.finish();
                }
            },2000);
        }else {
            /*
             *  timerTask: 计时器任务
             * delay: 延时1秒执行
             * period: 执行周期间隔为1秒
             * */
            timer.schedule(timerTask, 1000,1000);
        }
    }

    /*
     * 给按钮添加监听
     * */
    public void setClick(){
        btnSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timer.cancel();
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
                SplashActivity.this.finish();
            }
        });
    }

    /*
     * 在计时器中用handler给messageQueen发送消息
     * */
    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            handler.sendEmptyMessage(0);
        }
    };

    /*
     * 判断消息来源,实现倒计时
     * */
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0){
                num--;
                if(num < 0){
                    timer.cancel();
                    startActivity(new Intent(SplashActivity.this,MainActivity.class));
                    SplashActivity.this.finish();
                }else {
                    btnSkip.setText("跳过"+" "+String.valueOf(num));
                }
            }
        }
    };

}

activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="bottom">

    <Button
        android:id="@+id/btn_skip"
        android:layout_width="80dp"
        android:layout_height="45dp"
        android:text="跳过 3"
        android:textColor="#FFF"
        android:textSize="18sp"
        android:background="@drawable/splash_btn_shape"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:layout_gravity="right">

    </Button>


</LinearLayout>

 

android 启动页

上一篇:Linux基本指令,Java笔试面试宝典


下一篇:通用Mapper(十四)通用Mapper接口大全