2017-09-27 99 views
1

我正在開發一個android應用程序,它有一個運行時間爲2500ms的Splash Screen。 我想爲用戶觸摸屏添加功能並跳過此活動。SplashScreen跳過觸摸事件

我可以加一個按鈕,但對於相當客觀的我只想補充觸屏監聽器(不知道怎麼辦。)

我閃屏:

public class Splash extends Activity { 

// Splash screen timer 
private static int SPLASH_TIME_OUT = 2500; 

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

    /* 
    * Showing splash screen with a timer. This will be useful when you 
    * want to show case your app logo/company 
    */ 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      startActivity(new Intent(Splash.this, MainActivity.class)); 
      finish(); 
     } 
    }, SPLASH_TIME_OUT); 

//Skip this intro 
     RelativeLayout root_layout = (RelativeLayout) findViewById(R.id.root_splash); 
     root_layout.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       startActivity(new Intent(Splash.this, MainActivity.class)); 
       finish(); 
       return true; 
      } 
     }); 
} 
} 

我splashactivity佈局:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root_splash" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/splash_screen"> 

</RelativeLayout> 

回答

0

添加全屏視圖命名ll_root在R.layout.splashactivity,然後在飛濺活動使用findViewById和setOnTouchListener爲ll_root。

+0

好吧,那是邏輯,但是如何在代碼中? –

+1

在splashactivity佈局中爲根RelativeLayout提供一個id,並通過finViewById()方法獲取根視圖,然後爲根視圖設置touch偵聽器。如果它對你有幫助,請註冊。 – namezhouyu

+0

我做到了,但onTouchListener根本沒有owrk,更新代碼修復... –

1

刪除啓動畫面。這是2007年。

+0

Jajaja,這是一個有趣的答案。我同意,但古巴的用戶喜歡那種舊時尚的行爲。 –

0

在啓動屏幕布局上添加onTouchListener並在活動中致電您的班級。 希望它會爲你

由於工作..