简体   繁体   English

如何只显示几秒钟的屏幕 Android Studio

[英]How to display a screen only for some seconds Android Studio

I am new in Android Development.我是 Android 开发的新手。 I have been trying to figure out how to display a screen in android studio only for 5 seconds and then get transfered into a new activity.我一直试图弄清楚如何在 android 工作室中仅显示 5 秒钟的屏幕,然后转移到新的活动中。

For example: Activity A -> Activity B (Shown for 5 seconds) -> Activity C例如:活动 A -> 活动 B(显示 5 秒) -> 活动 C

Also I want to make sure that when a user clicks on the back button while he is in Activity B nothing happens (It doesnt go back to Activity A).另外我想确保当用户在活动 B 中单击后退按钮时,没有任何反应(它不会 go 回到活动 A)。

What is the easiest way to do that?最简单的方法是什么? I know I have to use Intent.我知道我必须使用 Intent。

try this.尝试这个。 I have commented it out, but if you have any questions about it feel free to ask.我已将其注释掉,但如果您对此有任何疑问,请随时提出。

public class ClassB extends AppCompatActivity {


//Handler allows you to send and process Runnable Objects (Classes in this case)
private Handler mHandler = new Handler();


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


    //postDelayed method, Causes the Runnable r (in this case Class B) to be added to the message queue, to be run
    // after the specified amount of time elapses.
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Create a new Intent to go from Class B to Class C and start the new Activity.
            Intent intent = new Intent(ClassB.this, ClassC.class);
            startActivity(intent);
            finish()

        }
        //Here after the comma you specify the amount of time you want the screen to be delayed. 5000 is for 5 seconds.
    }, 5000);
}

//Override onBackPressed method and give it no functionality. This way when the user clicks the back button he will not go back.
public void onBackPressed() {

} }

In Kotlin you can do:在 Kotlin 你可以这样做:

 Handler().postDelayed({
  // Start activity
  startActivity(Intent(this, YourTargetActivity::class.java))
  // terminate this activity(optional)
  finish()
}, 5000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM