如何只显示屏幕几秒钟安卓工作室



我是安卓开发的新手。我一直在想如何在安卓工作室中只显示5秒的屏幕,然后转移到一个新的活动中。

例如:活动A->活动B(显示5秒(->活动C

此外,我想确保当用户在活动B中单击后退按钮时,不会发生任何事情(不会返回到活动a(。

最简单的方法是什么?我知道我必须使用Intent。

试试这个。我已经评论过了,但如果你对此有任何问题,请随时提问。

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() {
} }

在Kotlin你可以做:

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

最新更新