menu event handling

2019-08-23 05:10发布

I want to set a list of choice after selected the language choice in menu list.

The list works in the normal button case (onClick) but menu.

Anything I should notice?

sorry for my english

switch (item.getItemId()) {
        case R.id.changeCity:
            showInputDialog();
            return true;
        case R.id.Language:
            List<String> language = new ArrayList<String>();
            language.add(getString(R.string.english));
            language.add(getString(R.string.chinese));
            new AlertDialog.Builder(WeatherClass.this)
                    .setSingleChoiceItems(language.toArray(new String[language.size()]), 0,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            })
                    .show();
              return true;
        default:
            return super.onOptionsItemSelected(item);
    }

what I want

enter image description here
enter image description here
Show the list like enter image desdwcription here

1条回答
乱世女痞
2楼-- · 2019-08-23 05:33

First show dialog of language selection and according to position or text show another dialog with content.

 case R.id.Language:
   final CharSequence[] items = { "English", "Chinese"};

   AlertDialog.Builder builder = new AlertDialog.Builder(WeatherClass.this);
   builder.setTitle("Selection");
   builder.setItems(items, new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int position ) {

           List<String> language = new ArrayList<String>();

           if (position == 0) {           
                // language = "english data"
           }
           if (position == 1) {       
                // language = "chinese data"        
           }
           new AlertDialog.Builder(WeatherClass.this).setSingleChoiceItems(language.toArray(new String[language.size()]), 0, new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                           // second dialog data
                  }
            })
          .show();
       }
   }).show();
  return true;
查看更多
登录 后发表回答