如何在几秒钟内转移到另一个活动?

时间:2012-08-28 13:28:49

标签: android android-activity timer

我有一个闪屏。我只是希望它等待1或2秒,然后继续进行下一个活动。 我知道有很多方法,包括处理程序类和java.util.timer实现。但这是最简单,最轻松的方法。提前完成。

2 个答案:

答案 0 :(得分:14)

使用以下代码。

Splash_Screen_Activity.java

public class Splash_Screen_Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                // TODO: Your application init goes here.
                Intent mInHome = new Intent(Splash_Screen_Activity.this, InvoiceASAPTabActivity.class);
                Splash_Screen_Activity.this.startActivity(mInHome);
                Splash_Screen_Activity.this.finish();
            }
        }, 3000);
    }
}

答案 1 :(得分:0)

这是一个例子,其中包括淡入淡出效果。

<强> RES /过渡/ fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="2000" />

<强> RES /过渡/ fade_out.xml     

<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="2000" />

<强> MainActivity.class

public class MainActivity extends AppCompatActivity {

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

        final android.os.Handler handler = new android.os.Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                overridePendingTransition(R.transition.fade_in,R.transition.fade_out);
            }
        }, 3000);
    }

}

如需完整的应用示例(包含更多功能),请检查here