2012-10-23 95 views
0

我开发一个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; 
} 

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

+1

为什么两个'onOptionsItemSelected'?只需使用'switch case'来查看选择了哪个菜单并开始相应的活动。 –

+0

是第一个menuitem工作,第二个不工作? –

+0

在onOptionsItemSelected.Updating代码中使用开关。 –

回答

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


    } 

    }