android - 如何在菜单项上做点击事件?

标签 android android-layout menuitem

我在 TextBox 中输入所有数据,当我按下提交按钮时它正在工作,但我在 Menuitem 按钮上做同样的事情然后它不起作用。 我的代码是:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG,"Information - onCreate() ...");
    setContentView(R.layout.activity_main);

    View promptButton = findViewById(R.id.submit_button);
    promptButton.setOnClickListener((OnClickListener) this);

}
public void onClick(View v) {
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.greetingsRadioGroup);
    int checkedRadioButton = radioGroup.getCheckedRadioButtonId();


    switch (checkedRadioButton) {
    case R.id.mrButton : 
      choice = "Mr.";
      break;
  case R.id.mrsButton : 
      choice = "Mrs.";
      break;
  case R.id.msButton: 
      choice = "Ms.";
      break;
  case R.id.drButton: 
      choice = "Dr.";
      break;
    }


         EditText namef = (EditText) findViewById(R.id.firstname); 
         EditText namel = (EditText) findViewById(R.id.lastname);  
         EditText add1 = (EditText) findViewById(R.id.address); 
         Spinner pro1 = (Spinner) findViewById(R.id.spinner1);  
         EditText coun1 = (EditText) findViewById(R.id.country);  
         EditText pcode1 = (EditText) findViewById(R.id.postalCode); 

            switch(v.getId()){
                case R.id.submit_button:
                    Intent j = new Intent(this, Animation.class);
                    fname = namef.getText().toString();
                    lname = namel.getText().toString();
                    add = add1.getText().toString();
                    pro = pro1.getSelectedItem().toString();
                    coun = coun1.getText().toString();
                    pcode = pcode1.getText().toString();
                    j.putExtra("choice", choice);
                    j.putExtra("fname", fname);
                    j.putExtra("lname", lname);
                    j.putExtra("add", add);
                    j.putExtra("pro", pro);
                    j.putExtra("coun", coun);
                    j.putExtra("pcode", pcode);
                    startActivity(j);
                    break;  
                case R.id.menu_anim:
                    Intent k = new Intent(this, Animation.class);
                    fname = namef.getText().toString();
                    lname = namel.getText().toString();
                    add = add1.getText().toString();
                    pro = pro1.getSelectedItem().toString();
                    coun = coun1.getText().toString();
                    pcode = pcode1.getText().toString();
                    k.putExtra("choice", choice);
                    k.putExtra("fname", fname);
                    k.putExtra("lname", lname);
                    k.putExtra("add", add);
                    k.putExtra("pro", pro);
                    k.putExtra("coun", coun);
                    k.putExtra("pcode", pcode);
                    startActivity(k);
                    break;

         }


    }
}


protected void onActivityResult(int requestCode, int resultCode, Intent d1) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, d1);

    if(requestCode == START)
    {
        Toast.makeText(this, "Your download has resumed.", Toast.LENGTH_LONG).show();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

   super.onCreateOptionsMenu(menu);

    this.myMenu = menu;

    MenuInflater mi = this.getMenuInflater();
    mi.inflate(R.menu.my_menu,menu);

    return true;
}

我不知道我可以在这个switch Case R.id.menu_anim...中写什么

我可以调用 onClick(View v) 吗? 如果是,我该如何调用它?

private boolean handleMenus(MenuItem item)
{
    switch (item.getItemId()) {
    case R.id.menu_anim:

        break;
    }

return true;
}

最佳答案

下面是如何创建 OptionMenu 并在菜单项上设置 ClickAction 的示例:

// for Menu
private static final int SETTING_ID = Menu.FIRST;
private static final int EXISTING_GAMES_ID = Menu.FIRST + 1;
private static final int GAME_SELECTION_ID = Menu.FIRST + 2;


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    menu.add(0, SETTING_ID, 0, "settings");
    menu.add(0, EXISTING_GAMES_ID, 0, "existing games");
    menu.add(0, GAME_SELECTION_ID, 0, "new game");
    /****   Is this the mechanism to extend with filter effects?
    Intent intent = new Intent(null, getIntent().getData());
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    menu.addIntentOptions(
                          Menu.ALTERNATIVE, 0,
                          new ComponentName(this, NotesList.class),
                          null, intent, 0, null);
    *****/
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   /* mPaint.setXfermode(null);
    mPaint.setAlpha(0xFF);*/

    switch (item.getItemId()) {
        case SETTING_ID:
            //Do Some Task

            return true;
        case EXISTING_GAMES_ID:
            //Do Some Task
            return true;
        case GAME_SELECTION_ID:
            //Do Some Task
            return true;

    }
    return super.onOptionsItemSelected(item);
}

关于android - 如何在菜单项上做点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13260157/

相关文章:

android - DJI Mobile Android SDK 是否支持三星 S7、S8 或 Google Pixel?

xml - 似乎无法从 xml 加载 webview,为什么?

用于许多额外详细信息的 Android 表单

android - 更改布局颜色android应用程序

android - Adobe Flex 如何在 Android/iOS 上运行

安卓NDK :linking static library with shared library

android - 按钮不是从 XML 中获取的?空指针

WPF 数据绑定(bind) IsEnabled 属性

java - Blackberry:更改 KeywordFilterField 的排序

android - NavigationView:使用 app:actionLayout 自定义项目布局如何?