2011-08-23 4 views
1

J'ai créé un menu déroulant qui affiche deux boutons, un lien vers une liste d'éléments cliquables, qui, une fois cliqué, ferme le menu et retourne à l'écran principal. vue d'écran. Cependant, l'autre bouton affiche une liste de boutons radio qui fonctionnent mais la touche de retour doit être pressée pour fermer ce menu. Je veux qu'il se ferme automatiquement une fois que l'une des options a été sélectionnée.Comment fermer mon menu inflatable après la sélection d'une option

Toutes les suggestions sur la façon de rendre cela possible seraient grandement appréciées, merci d'avance pour toute aide.

Voici mon code:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { //creates the menu options that appear when the menu button is pressed 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.mainmenu, menu); 
    return true; 
} 
public boolean onOptionsItemSelected(MenuItem item) { // assigns one button in the menu to display Rhythm and then further options 

    final CharSequence[] Rhythms = {"Sinus Rhythm", "Atrial Fibulation", "Atrial Flutter", "Junctional Rhythm", "SVT", "Ventricular Tachycardia"}; 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    builder.setTitle("Interpretation of ECG waveform"); 
    builder.setItems(Rhythms, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int Rhythm) { 
      Toast.makeText(getApplicationContext(), Rhythms[Rhythm], Toast.LENGTH_SHORT).show(); // add a save function here to utilise the onclick function 
     }                      // saved file should match the ecg file name and also be loaded when app is started 
    }); 
    final CharSequence[] annotations = {"A", "B", "B-","C", "C-", "D", "F"}; // assigns a new button with the annotation options 

    AlertDialog.Builder builder1 = new AlertDialog.Builder(this); 
    builder1.setTitle("Evaluate ECG quality"); 
    builder1.setSingleChoiceItems(annotations, 0, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      Toast.makeText(getApplicationContext(), annotations[item], Toast.LENGTH_SHORT).show(); // again add a save function here, this should be able to override the 
                            // annotation coding written by Daniel. 
     } 
    }); 

      setCancelable(true);  

    switch (item.getItemId()) { 

    case R.id.annotatebutton: // when annotation button is click display annotation options 
     builder1.show(); 
     return true; 

    case R.id.rhythmbutton: // when rhythm button is clicked display rhythm options 
     builder.show();   
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

Répondre

Questions connexes