menuActivity.java 内容
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
button1=(Button) findViewById(R.id.buttons);
button2=(Button) findViewById(R.id.buttons2);
button1.setVisibility(View.INVISIBLE);
button2.setVisibility(View.INVISIBLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "显示buttons1");
menu.add(0, 1, 1, "显示buttons1");
menu.findItem(1);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId()){
case 0:button2.setVisibility(View.INVISIBLE);
button1.setVisibility(View.VISIBLE);
break;
case 1:button1.setVisibility(View.INVISIBLE);
button2.setVisibility(View.VISIBLE);
break;
}
return super.onOptionsItemSelected(item);
}
xml文件中内容
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is the menu text" />
<Button android:id="@+id/buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="buttons" />
<Button android:id="@+id/buttons2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="buttons2" />