【发布时间】:2016-01-04 16:06:16
【问题描述】:
点击一个按钮后,它会调用一个函数 show 从中出现一个对话框。我希望对话框保持大约 4 秒钟,然后关闭并打开另一个活动。使用我编写的代码,它会等待 4 秒,显示对话框并立即进入下一个活动。
public void show()
{ final Dialog dialog = new Dialog(invoice.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.popup);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = Math.round((width * 10) / 15);
lp.height = Math.round(height / 3);
dialog.getWindow().setAttributes(lp);
Intent intent = new Intent(invoice.this, First_grid.class);
intent.putExtra("yo", 0);
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= 16) {
//The code below is for the cool transition effect that happens once the player clicks the play button..!
Bundle translateBundle = ActivityOptions.makeCustomAnimation(invoice.this,
R.anim.activity_slide_up, R.anim.activity_slide_out_upwards).toBundle();
startActivity(intent, translateBundle);
dialog.dismiss();
finish();
} else {
startActivity(intent);
dialog.dismiss();
finish();
}
dialog.show();
}
【问题讨论】:
-
@FrankN.Stein 我应该在哪里做?
-
可能在 Activity
onCreate()中(我也会从那里打开对话框) -
将您的
dialog.show()放在Thread.sleep()之前并在try catch块之后添加一个finally并将您的if/else放在您的if/else
标签: android multithreading dialog runnable countdowntimer