选择选项后如何关闭充气机菜单

发布于 2024-12-01 02:33:07 字数 2482 浏览 2 评论 0原文

我创建了一个充气机菜单,它会显示两个按钮,其中一个链接到可单击项目列表,单击该按钮会关闭菜单并返回到主屏幕视图。然而,另一个按钮会弹出一个单选按钮列表,这些单选按钮可以工作,但必须按下返回键才能关闭此菜单。我希望它在选择其中一个选项后自动关闭。

任何有关如何实现这一点的建议将不胜感激,提前感谢您的帮助。

这是我的代码:

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

I have created an inflater menu which brings up two buttons, one links to a list of clickable items, which when clicked closes the menu and returns to the main screen view. However the other button brings up a list of radio buttons which work but the return key has to be pressed to close this menu. I want it to just close automatically once one of the options has been selected.

Any suggestions on how to make this happen would be much appreciated, thanks in advance for any help.

Here's my 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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦妄 2024-12-08 02:33:07
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()

case R.id.annotatebutton:
    builder1.show();
    closeOptionsMenu();
    return true;
case R.id.rhythmbutton:
    builder.show();        
    closeOptionsMenu();
    return true;
default:
    return super.onOptionsItemSelected(item);

You can close the options menu programmatically with closeOptionsMenu()

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

雪花飘飘的天空 2024-12-08 02:33:07

添加这一行:
对话框.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);
    }

add this line:
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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文