package com.robert.sample13;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.os.Build;
public class MenuActivity extends Activity {
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
if
(savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container,
new PlaceholderFragment()).commit();
}
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the
menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu,
menu);
return true;
}
@Override
public boolean
onOptionsItemSelected(MenuItem item) {
Toast.makeText(this,
item.getTitle(), Toast.LENGTH_SHORT).show();
String
selectedMenu;
switch(item.getItemId()){
case
R.id.menu1:
selectedMenu="menu1";
break;
case
R.id.menu2:
selectedMenu="menu2";
break;
case
R.id.menu3:
selectedMenu="menu3";
break;
case
R.id.menu4:
selectedMenu="menu4";
break;
default:
selectedMenu="";
}
Toast.makeText(this,
selectedMenu, Toast.LENGTH_SHORT).show();
return true;
}
/**
* A placeholder fragment containing a simple
view.
*/
public static class
PlaceholderFragment extends Fragment {
public
PlaceholderFragment() {
}
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
View
rootView = inflater.inflate(R.layout.fragment_menu, container,
false);
return
rootView;
}
}
}
沒有留言:
張貼留言