2015-06-16 43 views
0

我有测验代码,如果back button被按下,alert显示并且应用程序关闭,这是应用程序的流程。 但我有问题,如果返回按钮按下,那么它没有响应并执行代码。 帮助我,谢谢。如果后退按钮被按下,警报显示和应用程序关闭

公共类GandaActivity延伸活动{

List<Question> quesList; 
ArrayList<Integer> questIdRandom; 
int score=0; 
int qid=0; 
int randomIdSoal=0; 
Question currentQ; 
TextView txtQuestion; 
RadioButton rda, rdb, rdc; 
Button butNext; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.ganda_layout); 
    DBHelper db=new DBHelper(this); 
    quesList=db.getAllQuestions(); 


    Collections.shuffle(quesList); 

    currentQ=quesList.get(qid); 
    txtQuestion=(TextView)findViewById(R.id.tQuestion); 
    rda=(RadioButton)findViewById(R.id.radioButton); 
    rdb=(RadioButton)findViewById(R.id.radioButton2); 
    rdc=(RadioButton)findViewById(R.id.radioButton3); 
    butNext=(Button)findViewById(R.id.button_next); 
    setQuestionView(); 
    butNext.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RadioGroup grp=(RadioGroup)findViewById(R.id.groupRadio); 
      RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId()); 

      if(grp.getCheckedRadioButtonId() == -1){ 

      } 
      else if(grp.getCheckedRadioButtonId() != -1) { 
       grp.clearCheck(); 
       if (currentQ.getANSWER().equals(answer.getText())) { 
        //Log.d("yourans", currentQ.getANSWER()+" "+answer.getText()); 
        score++; 
        Log.d("score", "Your score" + score); 
       } 
       if (qid < 10) { 
        currentQ = quesList.get(qid); 
        setQuestionView(); 
       } else { 
        Intent intent = new Intent(GandaActivity.this, ResultQuizGanda.class); 
        Bundle b = new Bundle(); 
        b.putInt("score", score); //Your score 
        intent.putExtras(b); //Put your score to your next Intent 
        startActivity(intent); 
        finish(); 
       } 
      } 

     } 
    }); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 
private void setQuestionView() 
{ 
    txtQuestion.setText(currentQ.getQUESTION()); 
    rda.setText(currentQ.getOPTA()); 
    rdb.setText(currentQ.getOPTB()); 
    rdc.setText(currentQ.getOPTC()); 
    qid++; 
} 

private void setRandomSoal(){ 
    int size = quesList.size(); 
    int sizeRandom = 0; 
    Log.d("TOTAL SOAL","Jumlah soal "+size); 

    while(sizeRandom<10){ 
     int id = new Random().nextInt(size); 
     if(sizeRandom==0){ 
      questIdRandom.add(id); 
     }else if(cekQuestRandomId(id)){ 
      questIdRandom.add(id); 
     } 
     sizeRandom = questIdRandom.size(); 
    } 


} 

private boolean cekQuestRandomId(int id){ 
    boolean cek = false; 

    int sizeRandom = 0; 
    int i = 0; 
    while(sizeRandom<10){ 
     if(sizeRandom==0){ 
      cek = false; 
     }else if(questIdRandom.get(i)==id){ 
      cek = false; 
     }else{ 
      cek = true; 
     } 
     sizeRandom = questIdRandom.size(); 
     i++; 
    } 
    return cek; 
} 
public boolean onKeyDown(int keyCode, KeyEvent event){ 
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){ 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Apakah anda yakin ingin keluar?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         finish(); 
        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 

     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

}

谢谢,请救救我!

+0

可以请你上传日志。 – SAM

+0

你确定控制进入'if'部分。 'getRepeatCount == 0'? –

回答

2

覆盖onBackPressed()方法然后移动代码的onKeyDown()方法里面onBackPressed()

@Override 
public void onBackPressed(){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Apakah anda yakin ingin keluar?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         finish(); 
        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 
     builder.create().show(); 

} 

我加入这个builder.create().show();到您的代码,以便能够显示对话框。

相关问题