Как мне закрыть меню инфлатера после выбора опции? - PullRequest
2 голосов
/ 23 августа 2011

Я создал меню для всплывающих окон, в котором отображаются две кнопки, одна из которых связана со списком интерактивных элементов, при нажатии на которые меню закрывается и возвращается к главному экрану.Тем не менее, другая кнопка вызывает список переключателей, которые работают, но необходимо нажать клавишу возврата, чтобы закрыть это меню.Я хочу, чтобы он просто автоматически закрывался после выбора одного из вариантов.

Буду очень признателен за любые предложения о том, как это сделать, заранее спасибо за любую помощь.

Вот мой код:

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

Ответы [ 2 ]

3 голосов
/ 23 августа 2011
case R.id.annotatebutton:
    builder1.show();
    closeOptionsMenu();
    return true;
case R.id.rhythmbutton:
    builder.show();        
    closeOptionsMenu();
    return true;
default:
    return super.onOptionsItemSelected(item);

Вы можете закрыть меню опций программно с помощью closeOptionsMenu ()

см. http://developer.android.com/reference/android/app/Activity.html#closeOptionsMenu()

0 голосов
/ 27 марта 2018

добавить эту строку: dialog.dismiss ();

    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);
    AlertDialog alert;
    builder1.setTitle("Evaluate ECG quality");
    builder1.setSingleChoiceItems(annotations, -1, new DialogInterface.OnClickListener() { //change it by -1
        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
            dialog.dismiss(); //add this line
            // annotation coding written by Daniel.
        }
    });

    final CharSequence[] Rhythms = {"Sinus Rhythm", "Atrial Fibulation", "Atrial Flutter", "Junctional Rhythm", "SVT", "Ventricular Tachycardia"};
    final 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
    });

    switch (item.getItemId()) {

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

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