如何使CountDown计时器没有按钮

时间:2017-08-15 09:29:38

标签: java xml android-layout

我正在制作测验应用程序,我需要在问题得到解答的时候制作 CountDown计时器,但我不知道如何在不按任何按钮或制作的情况下制作CountDown计时器带按钮的CountDown计时器,但按钮位于不同的布局上。那么,我想问一下如何在没有按钮的情况下制作一个CountDown定时器,或者如何使用一个与倒数计时器不同的布局来制作倒数计时器?

**this is my code for the count down with button in other layout but still failed**

id = mulai是来自其他布局的按钮

id = waktu是将在其布局中倒计时的文本视图

public class Tes1 extends AppCompatActivity {

CheckBox cb1, cb2, cb3;
int nilai = 0;
private AlertDialog.Builder alertDialogBuilder;

TextView time;
private Button start;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tes1);

    start = (Button) findViewById(R.id.mulai);
    time = (TextView) findViewById(R.id.waktu);

    start.setOnClickListener(new View.OnClickListener(){
        public void onClick(View arg0){
            CountDownTimer count = new CountDownTimer(15 * 1000, 1000) {
                @Override
                public void onTick(long l) {
                    time.setText("" + l/1000);
                }

                @Override
                public void onFinish() {
                    time.setText("Times Up");

                }
            }.start();
                                 }
                             });

    alertDialogBuilder = new AlertDialog.Builder(this);

    cb1 = (CheckBox) findViewById(R.id.checkbox1);
    cb2 = (CheckBox) findViewById(R.id.checkbox2);
    cb3 = (CheckBox) findViewById(R.id.checkbox3);

}


public void nextButton(View view) {
    boolean checkbox1 = cb1.isChecked();
    boolean checkbox2 = cb2.isChecked();
    boolean checkbox3 = cb3.isChecked();

    int tambah = hitungNilaiCheckBox(checkbox1, checkbox2, checkbox3);

    Intent intent = new Intent(getApplicationContext(), Tes2.class);
    intent.putExtra("lempar_1", nilai);
    startActivity(intent);
}

private int hitungNilaiCheckBox(boolean checkbox1, boolean checkbox2, boolean checkbox3) {
    if (checkbox1) {
        nilai = nilai + 3;
    }
    if (checkbox2) {
        nilai = nilai + 2;
    }
    if (checkbox3) {
        nilai = nilai + 1;
    }

    return nilai;
}

public void onBackPressed() {

    alertDialogBuilder.setTitle("Tidak diperbolehkan");
    alertDialogBuilder.setMessage("Anda sedang di dalam tes")
            .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            }).create().show();
}

}

1 个答案:

答案 0 :(得分:0)

@Override
    protected void onStart()
   {
       super.onStart();
       Toast.makeText(getApplicationContext(),"Replace this delicious medium-brown toast with call to your timer", Toast.LENGTH_LONG).show(); //      
   }