2015-06-16 86 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();到您的代碼,以便能夠顯示對話框。

相關問題