【问题标题】:How do I get my inflater menu to close after an option has been selected选择选项后如何关闭充气菜单
【发布时间】:2011-08-23 10:08:35
【问题描述】:

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

任何关于如何实现这一点的建议将不胜感激,在此先感谢您的帮助。

这是我的代码:

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

【问题讨论】:

    标签: android menu layout-inflater radiobuttonlist


    【解决方案1】:
    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()

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 虽然此代码可以回答问题,但提供有关 如何为什么 解决问题的附加上下文将提高​​答案的长期价值。
      猜你喜欢
      • 1970-01-01
      • 2015-05-28
      • 2022-01-05
      • 2018-12-24
      • 1970-01-01
      • 2019-02-17
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多