2014-05-02 6 views
0

Je suis en train de programmer un jeu simple, et maintenant, je veux que mon application affiche une boîte de dialogue, quand un compte à rebours se termine. Je tryed ce code (voir ci-dessous), mais il ne fonctionne pas:Comment terminer une activité avec une boîte de dialogue

protected void alertbox() 
    { 
    new AlertDialog.Builder(this) 
     .setMessage("You've scored " + score + " points!") 
     .setTitle("Congatulations!") 
     .setCancelable(false) 
     .setNeutralButton(android.R.string.cancel, 
     new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton){} 
     ButtonActivity.this.finish(); 
     }) 
     .show(); 
    } 
+0

Avez-vous lu http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in- android – gahfy

+0

Appelez 'finish()' lorsque vous cliquez sur "OK"? – codeMagic

+0

"mais ça ne marche pas" - qu'est-ce que ça veut dire? il se bloque? Rien ne se passe? etc ?, poster la trace de la pile –

Répondre

1

Essayez ceci:

new AlertDialog.Builder(context).setMessage("Congratulations! You scored " + score + " points!") 
.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
{ 
    @Override 
    public void onClick(DialogInterface dialog, int which) 
    { 
     finish(); 
    } 
}).show(); 
1

faire comme ça:

final Dialog dialog = new Dialog(ButtonActivity.this); 
    dialog.setContentView(R.layout.dialog); 
    dialog.setTitle("Dialog Title"); 
    TextView text = (TextView) dialog.findViewById(R.id.text); 
    text.setText("Congratulations! You scored " + score + " points!"); 
    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); 

    dialogButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     dialog.dismiss(); 
     ButtonActivity.this.finish(); 
     } 
    }); 
    dialog.show(); 

dialog.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 



    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#FFF" 
     /> 

    <Button 
     android:id="@+id/dialogButtonOK" 
     android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:text="Ok" 
     android:layout_marginTop="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_below="@+id/text" 
     /> 

</RelativeLayout> 
0
protected void alertbox(){ 
    new AlertDialog.Builder(this) 
    .setMessage("You've scored " + score + " points!") 
    .setTitle("Congatulations!") 
    .setCancelable(false) 
    .setNeutralButton(android.R.string.cancel, 
    new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton){ 
     Intent intent =new Intent(getApplicationContext(), NextActivityClassName.class); 
     intent.putExtra("ID",score); 
     startActivity(intent); 
     finish(); 
    } 
    }).show(); 

}

Dans la prochaine utilisation de l'activité:

int scoreValue= getIntent().getIntExtra("ID", defaultValueIntValue); 
Questions connexes