启动画面 - 如何?

Splash Screen - how to?

我的启动画面有问题,因为应用程序启动后我首先看到 MainActivity,然后是启动画面,错误是什么?

public class MainActivity extends AppCompatActivity {

int time = 3000;

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

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(MainActivity.this, Splash_screen.class);
            startActivity(i);
            finish();
        }
    }, time);

XML MainActivity 代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<ImageButton
    android:id="@+id/b10"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginTop="450dp"
    android:layout_marginEnd="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b9"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="450dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b8"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginTop="250dp"
    android:layout_marginEnd="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b7"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="250dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginTop="50dp"
    android:layout_marginEnd="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b1"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="50dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

XML Splash_screen 的代码。

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Splash_screen">

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="427dp"
    android:layout_height="588dp"
    app:srcCompat="@drawable/beb"
    tools:layout_editor_absoluteX="-8dp"
    tools:layout_editor_absoluteY="16dp" />

这些是 MainActivity 文件和 Splash_screen 的 xml 代码。 感谢您的帮助,我是 android studio 和 Stack Overflow

的新手

首先去清单并像那样更改 intent-filter

<activity android:name=".SPLASH">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<activity android:name=".MainActivity" >

    </activity>

这里是你需要先启动程序才能启动启动画面 activity,而不是 splash.java

int time = 3000;
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent i = new Intent(Splash_screen.this, MainActivity.class);
        startActivity(i);
        finish();
    }
}, time);

您是从 MainActivity 调用启动画面,因此创建 activity 并显示启动画面是正常的;你应该按照@moon 的回答,但是有点不对:

您必须编辑 AndroidManifest.xml 以使 Splash_creen 首先被调用(确保 Splash_screenActivity):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="your.pack.age"
    android:installLocation="preferExternal" >

    <!-- Your permissions -->

    <application
        android:allowBackup="true"
        android:icon="@drawable/app_image" <!-- You'll have to modify these 3 lines -->
        android:label="@string/app_name"
        android:roundIcon="@drawable/app_round_image"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".Splash_screen">
            <intent-filter>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.pitibi.boulas.MainActivity"/>

    </application>
</manifest>

然后你从 Splash_screenOnCreate 调用 MainActivity:

public class Splash_screen extends AppCompatActivity {

int time = 3000;

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

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(Splash_screen.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }, time);