$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中的代码不宜过多。
随机推荐
- CI框架基本配置/教你学习CI框架codelgniter
CI框架现在中国可以说还是不成熟,不像thinkphp那样有那么多的中文手册,在国内,很多国人英语都很烂,CI现在教程还是不多.大家心里都存在这严重想法 CI 框架现在中国可以说还是不成熟,不像thi ...
- Redis Scan的使用方式以及Spring redis的坑
SpringRedisTemplate针对这个Scan进行了封装,示例使用(针对最新库spring-data-redis-1.8.1.RELEASE): Set<Object> execu ...
- 安装IPFS并通过自己的域名访问
下载go-ipfs_v0.4.18_linux-amd64.tar.gz,我这个是linux,如果没有可以在这里下载: http://www.froms.top:8282/ipfs/QmZs9HdSS ...
- Eclipse 透视图(Perspective)
什么是透视图? 透视图是一个包含一系列视图和内容编辑器的可视容器.默认的透视图叫 java. Eclipse 窗口可以打开多个透视图,但在同一时间只能有一个透视图处于激活状态. 用户可以在两个透视图之 ...
- scikit-learn:3.4. Model persistence
參考:http://scikit-learn.org/stable/modules/model_persistence.html 训练了模型之后,我们希望能够保存下来,遇到新样本时直接使用已经训练好的 ...
- Android-ViewPagerIndicator框架使用——IconPageIndicator
前言:IconPageIndicator是将自定义的图片作为指示图标的,这里的图片使用xml实现的. 1.自己定义图片: <?xml version="1.0" encodi ...
- Python_Selenium之鼠标右键
Python_Selenium之鼠标右键 一.步骤: (以百度为例)获取百度网址 找到需要右键的元素(定位),xpath表达式为“//*[@id='lg']/img” 然后,右键选择“在新标签页中打开 ...
- BAPI、badi 和 LSMW 的相同点和不同点及具体操作
一开始badi.BAPI.LSMW关系极其混乱,好像都是传输数据, just transfer data from SAP system to non-SAP system or transfer d ...
- LeetCode Problem 136:Single Number
描述:Given an array of integers, every element appears twice except for one. Find that single one. Not ...
- 编写自己的cp命令
有时候要对整个目录做备份,修改cp1.c使得当两个参数都是目录时,把第一个目录中的所有文件复制到第二个目录中,文件名不变.那么该如何实现? 我们先来看看cp1.c的实现方式,它从一个文件中读取数据然后 ...