(一)用2个Activity实现

  用Handler对象的postDelayed方法来实现延迟跳转的目的。

  补充:Handler的常用方法:

 //  立即执行Runnable对象
public final boolean post(Runnable r);
// 在指定的时间(uptimeMillis)执行Runnable对象
public final boolean postAtTime(Runnable r, long uptimeMillis);
// 在指定的时间间隔(delayMillis)执行Runnable对象
public final boolean postDelayed(Runnable r, long delayMillis);

  1、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" > <ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" /> </LinearLayout>

  2、activity_main.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这里是主界面" /> </LinearLayout>

  3、SplashActivity:

 package com.example.splashtest;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window; public class SplashActivity extends Activity { private final int SPLASH_DISPLAY_LENGHT = 3000;
private Handler handler; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash); handler = new Handler();
// 延迟SPLASH_DISPLAY_LENGHT时间然后跳转到MainActivity
handler.postDelayed(new Runnable() { @Override
public void run() {
Intent intent = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(intent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGHT); }
}

  4、MainActivity:

 package com.example.splashtest;

 import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

  6、修改AndroidManifest.xml文件:

      ...
     <activity
android:name=".SplashActivity"
android:label="splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
     ...

  7、在SplashActivity中禁用返回键:

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
  if(keyCode == KeyEvent.KEYCODE_BACK){
    return true;
  }
  return super.onKeyDown(keyCode, event); }

  

  (二)用一个Activity实现

  主要利用控件的隐藏来实现。

  1、xml文件:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:id="@+id/splash_lt"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout> <TextView
android:id="@+id/main_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是主界面" /> </LinearLayout>

  2、MainActivity

 package com.example.splashtest2;

 import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout; public class MainActivity extends Activity { private final int STOP_SPLASH = 0;
private final int SPLASH_TIME = 3000; private LinearLayout splashLt; private Handler splashHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case STOP_SPLASH:
splashLt.setVisibility(View.GONE);
break;
default:
break;
} super.handleMessage(msg);
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); splashLt = (LinearLayout) findViewById(R.id.splash_lt); Message msg = new Message();
msg.what = STOP_SPLASH; // 注:这里必须用延迟发送消息的方法,否则ImageView不会显示出来
splashHandler.sendMessageDelayed(msg, SPLASH_TIME);
} }

  (三)小结

  建议使用第一种方法,用两个Activity实现,因为MainActivity中的代码不宜过多。

随机推荐

  1. SpringMVC学习(一)小demo

    首先看一下整个demo的项目结构: 第一步是导入Spring MVC单独使用时的最少jar包: 第二步在项目的web.xml中配置Spring MVC提供的拦截请求的Servlet: 全类名是:org ...

  2. windows中控制台窗口和普通窗口有什么区别?

    1. 窗口都是windows标准窗口,有窗口句柄,但是console window没有消息循环,直接从缓冲区读数据,显示数据. windows中普通窗口都有自己的窗口过程, 我可以使用SetWindo ...

  3. ios 2017启动页(Launch Screen Images)、图标(App Icon)尺寸大小

    ios 2017启动页(Launch Screen Images).图标(App Icon)尺寸大小   iPhone Portrait iOS 8,9-Retina HD 5.5 (1242×220 ...

  4. hdu1829(A Bug's Life)

    题目链接:传送门 题目大意:有n个昆虫,有m组关系,接下来m行表示两个昆虫性别不同,问是否有矛盾情况(同男同女) 题目思路:并查集的高级应用,开两倍数组大小,后n个数组表示和当前昆虫不同性别的集合 # ...

  5. Sequence I

    Sequence I (hdu 5918) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  6. IDEA中的lombok插件安装以及各注解的详细介绍

    IDEA中的lombok插件安装以及各注解的详细介绍 其实对于我们来说, 写好实体类后,直接用快捷方式生成get,set方法,还有 构造方法就行了,但是对于字段比较多的, 如果修改一个属性的话,就要再 ...

  7. 关于自我总结的html5新特性

    最近本包子制订了一个学校计划,第一步就是了解并总结一下html5现在所含有的新特性,好吧,这只是一个了解,- -! 自己总结了一个word文档,里面很多东西自己都还没实际用过,下一步,本包子要写pc端 ...

  8. Webshell清除-解决驱动级文件隐藏挂马

    Webshell清除-解决驱动级文件隐藏挂马

  9. Django的admin定制

    1,models编写 #encoding=utf-8 from django.db import models # Create your models here. class BookInfo(mo ...

  10. REST --- Representational State Transfer --- 表现层状态转化

    引用:阮一峰的网络日志 如果一个架构符合REST原则,就称它为RESTful架构. 要理解RESTful架构,最好的方法就是去理解Representational State Transfer这个词组 ...