没点击跳过自然进入d88尊龙官网手机app主页,点击跳过之后立即进入d88尊龙官网手机app主页
1.欢迎页布局activity_sp.xml放一张背景图(图片随你便啦)再放一个盛放倒计时的textview
2.主要java代码
package com.cwj.test; import android.content.intent; import android.os.handler; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.view.windowmanager; import android.widget.textview; import java.util.timer; import java.util.timertask; public class spactivity extends appcompatactivity implements view.onclicklistener { private int reclen = 5;//跳过倒计时提示5秒 private textview tv; timer timer = new timer(); private handler handler; private runnable runnable; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //定义全屏参数 int flag= windowmanager.layoutparams.flag_fullscreen; //设置当前窗体为全屏显示 getwindow().setflags(flag, flag); setcontentview(r.layout.activity_sp); initview(); timer.schedule(task, 1000, 1000);//等待时间一秒,停顿时间一秒 /** * 正常情况下不点击跳过 */ handler = new handler(); handler.postdelayed(runnable = new runnable() { @override public void run() { //从闪屏界面跳转到首界面 intent intent = new intent(spactivity.this, mainactivity.class); startactivity(intent); finish(); } }, 5000);//延迟5s后发送handler信息 } private void initview() { tv = findviewbyid(r.id.tv);//跳过 tv.setonclicklistener(this);//跳过监听 } timertask task = new timertask() { @override public void run() { runonuithread(new runnable() { // ui thread @override public void run() { reclen--; tv.settext("跳过 " reclen); if (reclen < 0) { timer.cancel(); tv.setvisibility(view.gone);//倒计时到0隐藏字体 } } }); } }; /** * 点击跳过 */ @override public void onclick(view view) { switch (view.getid()) { case r.id.tv: //从闪屏界面跳转到首界面 intent intent = new intent(spactivity.this, mainactivity.class); startactivity(intent); finish(); if (runnable != null) { handler.removecallbacks(runnable); } break; default: break; } } }
这里style里改了样式,没有标题栏theme.appcompat.light.noactionbar
补充:
android程序跳过登录界面直接进入主界面(自动登录)
首先是欢迎界面的代码
public class welcomeactivity extends activity{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.welcome); /** * 延迟3秒进入主界面 */ new handler().postdelayed(new runnable() { @override public void run() { intent intent=new intent(welcomeactivity.this,loginactivity.class); startactivity(intent); welcomeactivity.this.finish(); } },1000*3); } }
接下来是文章的主要内容。实现自动登录的关键是当程序从欢迎界面跳转到登录界面是,在登录界面还没有加载布局文件时判断是否登陆过,从而实现直接跳转到主界面。这里我们采用sharedpreferences来保存登录状态。代码如下:
public class loginactivity extends activity{ sharedpreferences sprfmain; sharedpreferences.editor editormain; button login; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //在加载布局文件前判断是否登陆过 sprfmain= preferencemanager.getdefaultsharedpreferences(this); editormain=sprfmain.edit(); //.getboolean("main",false);当找不到"main"所对应的键值是默认返回false if(sprfmain.getboolean("main",false)){ intent intent=new intent(loginactivity.this,mainactivity.class); startactivity(intent); loginactivity.this.finish(); } setcontentview(r.layout.login); login= (button) findviewbyid(r.id.login); //这里只是简单用按键模拟登录功能 login.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent=new intent(loginactivity.this,mainactivity.class); editormain.putboolean("main",true); editormain.commit(); startactivity(intent); loginactivity.this.finish(); } }); } }
接下来是实现注销后要重新进入登录界面
public class mainactivity extends appcompatactivity { sharedpreferences sprfmain; sharedpreferences.editor editormain; button exit; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); exit= (button) findviewbyid(r.id.exit); exit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //点击注销按键后调用loginactivity提供的resetsprfmain()方法执行editormain.putboolean("main",false);,即将"main"对应的值修改为false resetsprfmain(); intent intent=new intent(mainactivity.this,welcomeactivity.class); startactivity(intent); mainactivity.this.finish(); } }); } public void resetsprfmain(){ sprfmain= preferencemanager.getdefaultsharedpreferences(this); editormain=sprfmain.edit(); editormain.putboolean("main",false); editormain.commit(); } }