显示对话框几秒钟

时间:2016-01-04 16:06:16

标签: android multithreading dialog runnable countdowntimer

单击按钮后,它会调用一个功能节目,其中会出现一个对话框。我想对话框保持大约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();
        }

1 个答案:

答案 0 :(得分: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);
}