簡體   English   中英

啟動屏幕后啟動另一個活動,而不是mainactivity

[英]Launch another activity rather than mainactivity after splash screen

誰能告訴我如何使我的android應用程序執行此命令?

1)啟動屏幕(SplashActivity)..我作為啟動器做了

2)介紹滑塊(WelcomeActivity)..我不知道如何使它出現在飛濺之后。

3)主要活動..我希望它在歡迎之后出現,否則我將通過單擊“ GOT IT”按鈕啟動它。

提前致謝。

如果我是正確的,那么您要問的是如何使用意圖和處理程序。 首先,您的splashActivity.java應該如下所示;

public class SplashActivity extends Activity{

//timer in miliseconds, 1000ms = 1s//
private static int SPLASH_TIME_OUT = 2000;

//create first screen showed when app is launched//
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    new Handler().postDelayed(new Runnable() {
        //showing splashscreen with a timer //

        @Override
        public void run() {
            //this is executed once the timer is over//

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

        }
    },SPLASH_TIME_OUT);

}
}

然后例如在AndroidManifest.xml中聲明您的菜單活動和啟動活動;

<activity
        android:name=".SplashActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

然后,有關如何在歡迎活動之后打開您的主活動的方法,只需將SplashActivity.java的代碼復制並粘貼到WelcomeActivity中,進行必要的更改,但是有關如何使用按鈕打開的方法,請參見下面的示例代碼,首先顯示您的按鈕設計已經在您的activity_welcome.xml中,例如

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_welcome"
android:layout_width="match_parent"
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.domainname.yourappname.WelcomeActivity"
android:background="@drawable/splash"

<Button
        android:text="@string/got it"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:textAlignment="center"
        android:textSize="30sp"
        android:layout_marginTop="41dp"
    android:textColorHighlight="@android:color/transparent"
    android:textColorHint="@android:color/transparent"
    android:layout_below="@+id/textView3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

</RelativeLayout>

在您的WelcomeActivity.java中

public class WelcomeActivity extends Activity {
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    addListenerOnButton();

}
public void addListenerOnButton() {

    final Context context = this;

    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, MainActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(context, "MainActivity Opened.", Toast.LENGTH_SHORT).show();

        }

    });
}
}

注意:到目前為止,我不知道您正在編寫什么程序或如何設計,這只是一個示例,您可能需要進行調整才能使實際代碼正常運行

回答您的“ HOW”問題;

首先,確保在清單中聲明了所有活動,如下所示:

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

然后在啟動計時器結束時在SplashActivity中聲明以下內容:

    //If you're using a "Timer" to count down splash screen
new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, WelcomeActivity.class);
                startActivity(intent);

            }
        }, 2000);

在無論您在何處調用活動結束的WelcomeActivity中:

    Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
    startActivity(intent);

您可以在此處找到有關使用意圖啟動另一個活動的更多信息,包括如何為下一個活動接收額外的數據。 希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM