在操作栏上执行不同的操作

时间:2012-10-23 14:17:40

标签: android android-actionbar

我开发了一个Android应用程序,我在操作栏中有两个菜单,我为每个菜单做了一个动作,但我只能访问其中一个,这是我的代码

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    Toast.makeText(this, "Studentsite News", Toast.LENGTH_LONG).show();
    Intent i = new Intent(this, ssnews.class);
    startActivity(i);
    return true;
}
public boolean onOptionsItemSelected1(MenuItem item)
{
    Toast.makeText(this, "About", Toast.LENGTH_LONG).show();
    Intent i = new Intent(this, about.class);
    startActivity(i);
    return true;
}

你有任何想法解决它吗?我真的很感激。

2 个答案:

答案 0 :(得分:2)

通读此页:http://developer.android.com/guide/topics/ui/menus.html#RespondingOptionsMenu

本节演示了使用onOptionsItemSelected方法的理想方式(实际上,与Menus有关)。

页面摘录(与您的情况有关):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.new_game:
            newGame();
            return true;
        case R.id.help:
            showHelp();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

答案 1 :(得分:0)

  @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

      switch (item.getItemId()) {

        case 1:
    Toast.makeText(this, "Studentsite News", Toast.LENGTH_LONG).show();
    Intent i = new Intent(this, ssnews.class);
    startActivity(i);
    return true;               
        case 2:
      Toast.makeText(this, "About", Toast.LENGTH_LONG).show();
    Intent i = new Intent(this, about.class);
    startActivity(i);

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


     }

  }