如何运行启动画面,然后介绍滑块和主要活动

时间:2018-08-13 20:24:57

标签: java android

我想运行“启动画面”,“简介滑块”和“主要活动”之类的活动。但是它运行闪屏,但在闪屏后不运行介绍滑块。该如何解决?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.androidhive.introslider">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name="info.androidhive.introslider.Splashscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="info.androidhive.introslider.WelcomeActivity" />
        <activity
            android:name="info.androidhive.introslider.MainActivity"
            android:label="@string/title_activity_welcome"
            android:theme="@style/AppTheme.NoActionBar"/>
    </application>  
</manifest>

启动画面。java

package info.androidhive.introslider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Splashscreen extends Activity {
    private static int SPLASH_TIMER = 1000;
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
            new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(Splashscreen.this,MainActivity.class);
                startActivity(i);
                finish();
            }
        },SPLASH_TIMER);
    }
}

2 个答案:

答案 0 :(得分:0)

从SplashScreen启动WelcomeActivity。

    public class Splashscreen extends AppCompatActivity {

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


        startActivity(new Intent(this, WelcomeActivity.class));
        finish();

    }
}

然后在“简介”滑块活动中使用sharedpreference检查首次启动。首次启动后,您的应用将从初始屏幕移至MainActivity。

答案 1 :(得分:0)

SplashScreen.java ,这是您的第一个文件

    public class Splashscreen extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);
        new Timer().schedule(new TimerTask() {          
        @Override
        public void run() {

        Intent intent = new Intent(this, WelcomeActivity.class);
        startActivity(intent);
        finish();
         }
     }, 5000);

    }
}

WelcomeActivity.java

   public class WelcomeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);
        Button btn =findViewById(R.id.welcome_botton);

        btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);       
          }  
       });
    }
}

WelcomeActivity.xml

<Button
android:id="@+id/welcome_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button" />

MainActivity.java

 public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash_activity);

          // YOUR MAIN ACTIVITY LOGIC WRITE HERE
        }
    }

参考此层次结构并将所有活动注册到Manifest.xml文件中 这些由我创建的活动供您参考。