2011-08-23 56 views
1

我已經創建了一個充氣器菜單,它帶有兩個按鈕,其中一個鏈接指向可點擊項目列表,點擊時關閉菜單並返回主菜單屏幕視圖。然而,另一個按鈕會彈出一個單選按鈕列表,但必須按下返回鍵才能關閉此菜單。我希望一旦選擇了其中一個選項,它就會自動關閉。如何在選擇選項後讓我的充氣器菜單關閉

任何有關如何做到這一點的建議將非常感謝,提前感謝任何幫助。

這裏是我的代碼:

@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); 
    } 
} 

回答

相關問題