APP啟動(dòng)界面SplashActivity的兩種實(shí)現(xiàn)方法

第一種:用兩個(gè)activity實(shí)現(xiàn)

用Handler對象飛postDelayed方法來實(shí)現(xiàn)延遲跳轉(zhuǎn)的目的;
補(bǔ)充:
1 // 立即執(zhí)行Runnable對象
public final boolean post(Runnable r);

2 //在指定的時(shí)間uptimeMillis 執(zhí)行Runnable對象

public final boolean postAtTime(Runnable r, long uptimeMillis);

3 // 在指定的時(shí)間間隔 delayMillis 執(zhí)行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:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" 
    android:contentDescription="@string/hello_world"/>

</LinearLayout>

2,activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lyt.demo04_wakeuptimer.MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

3, SplashActivity.java

package com.lyt.demo04_wakeuptimer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
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);
    
    //隱藏標(biāo)題欄
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    
    //加載布局文件
    setContentView(R.layout.activity_splash);
    
    handler=new Handler();
    
    //延遲 SPLASH_DISPLAY_LENGHT時(shí)間然后跳轉(zhuǎn)到MainActivity
    handler.postDelayed(new Runnable() {
        
        @Override
        public void run() {
            Intent intent =new Intent();
            intent.setClass(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            SplashActivity.this.finish();
            
        }
    }, SPLASH_DISPLAY_LENGHT);  
}

/* 禁用返回鍵
 *(non-Javadoc)
 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode==KeyEvent.KEYCODE_BACK){
        
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

}

4, MainActivity.java
package com.lyt.demo04_wakeuptimer;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
/**

  • @author Administrator
  • 定時(shí)喚醒系統(tǒng)
  • 1,定時(shí)喚醒系統(tǒng)
  • 2,記錄3G狀態(tài)
  • 3,記錄GPS狀態(tài)
  • 4,

*/
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
}

}

5,修改AndroidManifest.xml文件:
    ...
2      <activity
3 android:name=".SplashActivity"
4 android:label="splash" >
5 <intent-filter>
6 <action android:name="android.intent.action.MAIN" />
7
8 <category android:name="android.intent.category.LAUNCHER" />
9 </intent-filter>
10 </activity>
11 <activity
12 android:name=".MainActivity"
13 android:label="@string/app_name" >
14 </activity>
15      ...

timg (2).jpg

第二種:方法 用一個(gè)activity實(shí)現(xiàn)Splash啟動(dòng)界面
用一個(gè)Activity實(shí)現(xiàn)**
  主要利用控件的隱藏來實(shí)現(xiàn)。
  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;

      // 注:這里必須用延遲發(fā)送消息的方法,否則ImageView不會顯示出來
      splashHandler.sendMessageDelayed(msg, SPLASH_TIME);
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容