【问题标题】:Show dialog box for a few seconds显示对话框几秒钟
【发布时间】: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


【解决方案1】:

试试这个,

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);    
    dialog.show();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(invoice.this, First_grid.class);
            intent.putExtra("yo", 0);
            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();
            }
        }
    }, 4000);
}

【讨论】:

  • 完美运行。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
相关资源
最近更新 更多