Android中的计时器弹出框/对话框

时间:2017-07-28 15:26:38

标签: android android-layout timer

当计时器结束时我正在尝试使用计时器我必须显示一个弹出框但是它不起作用活动会回到它调用的第一个活动,无论如何要做到这一点?我也试过警告框它也没有工作

 Timer t = new Timer();
        t.schedule(new TimerTask() {

            @Override
            public void run() {
                //alrt();
                final Dialog myDialog1 = new Dialog(getApplicationContext());
                myDialog1.setContentView(R.layout.register);
                myDialog1.setCancelable(false);
myDialog1.show();
                // If you want to call Activity then call from here for 5 seconds it automatically call and your image disappear....
            }
        }, 10000);
    }

这种活动的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="appsmash.keralapschelper.questionlist">


  <include layout="@layout/content_main"
      android:layout_marginLeft="0dp"
      app:layout_constraintLeft_toLeftOf="parent"
      android:id="@+id/include"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintBottom_toBottomOf="parent" />

  <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="TextView"
      app:layout_constraintBottom_toBottomOf="parent"
      android:layout_marginBottom="8dp"
      app:layout_constraintTop_toTopOf="parent"
      android:layout_marginTop="8dp"
      android:layout_marginLeft="310dp"
      app:layout_constraintLeft_toLeftOf="@+id/include"
      app:layout_constraintVertical_bias="0.039" />
</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

使用

new CountDownTimer(10000, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

        final Dialog myDialog1 = new Dialog(getApplicationContext());
        myDialog1.setContentView(R.layout.register);
        myDialog1.setCancelable(false);
        myDialog1.show();
    }
}.start();

答案 1 :(得分:0)

解决方案是 上课

AlertDialog alert;

和Oncreate()

 final AlertDialog.Builder dialog = new AlertDialog.Builder(this)
                .setTitle("Time is over").setMessage(
                        "Your score is"+test);
        dialog.setPositiveButton("Confirm",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
alert.dismiss();

                    }
                });
        alert = dialog.create();

和外面

new CountDownTimer(30000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                tim.setText("seconds remaining: " + millisUntilFinished / 1000);
                //here you can have your logic to set text to edittext
            }


            @Override
            public void onFinish() {
                // TODO Auto-generated method stub

                alert.show();
            }
        }.start();

    }