$Android启动界面(Splash)的两种实现方法
(一)用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中的代码不宜过多。
随机推荐
- raw flash vs FTL
1. 区别 raw flash devices: NAND, NOR, OneNAND, etc,其被作为MTD设备处理(既不是字符设备,也不是块设备). FTL device:Flash Trans ...
- 小米Note全网通支持7模19频:先发标准版
2015-06-26 16:42:53 17749 次阅读 9 次推荐 稿源:安卓中国 43 条评论 感谢安卓中国的投递 自古一入电信深似海,从此手机没法买.现在首台全网通小米手机即将诞生.6 月 2 ...
- 【转】windows下python开发环境搭建
1 -- 安装python的前期准备 Python开发有众多工具,又以Eclipse+Pydev最为常见.Eclipse平台对开发同学来讲,肯定是如雷贯耳,自不用废话.而PyDev是Eclipse平台 ...
- jetty端口灵活配置方法
在使用maven开发web项目极大地方便了jar包的依赖,在测试时也可以集成Servlet容器,从启动速度和量级上看,Jetty无疑是不二选择. 如果多个项目同时启动,就会端口冲突了. 一种办法是通过 ...
- oracle密码过期解决方法
Oracle提示错误消息ORA-28001: the password has expired 在oracle服务器上用sqlplus / as sysdba登录进去,可以通过下面的sql语句查看账户 ...
- hdu 2874(LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 思路:近乎纯裸的LCA,只是题目给出的是森林,就要判断是否都在同一颗树上,这里我们只需判断两个子 ...
- CodeIgniter框架——函数(时间函数、装载函数)剖析+小知识点
连接数据库: 格式: mysql -h主机地址 -u用户名-p用户密码 数据库的提示符:mysql> 退出数据库: exit(回车) 知识点积累: 1.date_default_timezone ...
- 【BZOJ1811】[Ioi2005]mea 乱搞
[BZOJ1811][Ioi2005]mea Description 考虑一个非递减的整数序列 S1,....Sn+1(Si<=Si+1 1<=i<=n). 序列M1...Mn是定义 ...
- 【BZOJ1997】[Hnoi2010]Planar 2-SAT
[BZOJ1997][Hnoi2010]Planar Description Input Output Sample Input 2 6 9 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 ...
- Springboot中读取自定义名称properties的
Springboot读取自定义的配置文件时候,使用@value,一定要指定配置文件的位置! 否则报错参数化异常!