(一)用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. MFC 控件RadioButton和CheckBox区别

    1. 单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox在选中后,通过点击可以变为未选中 2. 一组RadioButton,只能同时选中一个 一组CheckBox,能同时 ...

  2. Android startActivity()和onActivityResult()使用总结(转载)

    有三个Activity: A.java ,B.java ,C.java Activity之间的跳转常用方法: 1. startActivity(Intent intent); 该方法只用于启动新的Ac ...

  3. Hibernate使用Log4j日志记录(使用properties文件)

    我们知道,Log4j和Logback框架可用于支持日志记录hibernate,使用log4j有两种执行日志记录的方法: 通过log4j.xml文件(或) 通过log4j.properties文件 在这 ...

  4. SVN服务器迁移,SVN版本库迁移(网络copy)

    做法: 准备:系统平台:windows server 2003 版本库:vos 源服务器:10.10.13.48 目标服务器:10.10.13.129源SVN版本库的path: D:\svn\vos要 ...

  5. MEF基础概念学习笔记

    MEF,是微软.net框架下的一个框架类库.可以使你的程序低耦合的加载扩展.在开发插件,或者开发一些需要灵活扩展的功能的时候经常用到.例如微软给出的计算器的例子.当你开发计算器的时候,初始功能只提供了 ...

  6. OpenCV学习笔记十八:opencv_flann模块

    一,简介: Fast Library for Approximate Nearest Neighbors (FLANN)算法库.

  7. 排序算法 c实现

    c语言实现插入排序.冒泡排序.选择排序.快速排序.堆排序.归并排序.希尔排序示例,需要的朋友可以参考下     实现以下排序 插入排序O(n^2) 冒泡排序 O(n^2) 选择排序 O(n^2) 快速 ...

  8. F - 简单计算器(栈)

    F - 简单计算器 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descripti ...

  9. windows下在Eclipse中启动的tomcat没有乱码,单独部署到tomcat下乱码解决方案

    今天遇到了一个很奇怪的问题,在Eclipse中调试,运行项目一切正常,项目的所有编码都是统一的UTF-8.但是在单独部署到tomcat上的时候出现了中文乱码. 解决方案 第一步:确保项目,jsp页面, ...

  10. SharePoint服务器端对象模型 之 访问文件和文件夹(Part 4)

    (四)列表附件 列表的附件也是文件系统的一部分,它依附于普通列表的列表条目之上(文档库没有附件),它的操作在一些地方和文档库中文档的操作非常类似.   1.附件的读取 一个列表条目的附件可以使用SPL ...