我遇到onCreateOptionMenu()方法的问题。它不起作用。
我希望在我的活动有焦点时显示按钮。但没有任何事情发生。
我真的不明白。
PersonnalActivity:
public class PersonnalAccountActivity extends FragmentActivity {
private static final String TAG = "com.example.smartoo.Establishment.EstablishmentActivity";
private boolean wantGPS = false;
/** Drawer attr **/
private String[] mDrawerItemTitles;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private int positionDrawer;
public static int idUser = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences(LogInActivity.PREFS_NAME, MODE_PRIVATE);
idUser = settings.getInt("idUser", -1);
setContentView(R.layout.personnal_account_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
if (actionBar == null) {
Log.d(TAG, "actionBar is null !");
}
//Set up the left icon to go back to Home Page
actionBar.setDisplayHomeAsUpEnabled(true);
setUpDrawer();
Intent i = getIntent();
Bundle bundle = i.getExtras();
//If intent comes from home Activity
if(bundle.get("GPS") != null) {
wantGPS = bundle.getBoolean("GPS", false);
Log.d(TAG, "wantGPS 2 "+wantGPS);
}
//If intent comes from drawer button
if (bundle.get("DRAWER_ITEM") != null) {
positionDrawer = bundle.getInt("DRAWER_ITEM", 0);
mDrawerList.setItemChecked(positionDrawer, true);
setTitle(mDrawerItemTitles[positionDrawer]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
Log.d(TAG, "oncreateoptionsmenu personnal account actiity");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.actionbar, menu);
menu.findItem(R.id.menu_update).setVisible(true);
menu.findItem(R.id.menu_update).setEnabled(true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar etablishment_item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case android.R.id.home:
Intent result = new Intent(PersonnalAccountActivity.this, MapActivity.class);
result.putExtra("GPS", wantGPS);
setResult(RESULT_OK, result);
finish();
return true;
case R.id.menu_update:
PersonnalAccountFragment.setFieldstoEditText();
//getActionBar().setCustomView(R.layout.actionbar_layout);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/************************************************/
/***************** DRAWER ***********************/
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void setUpDrawer() {
//Get Drawer item titles
mDrawerItemTitles = getResources().getStringArray(R.array.drawer_array);
//Get DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//Get Listview into drawer
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mDrawerItemTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener(this));
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
// ActionBarDrawerToggle ties together the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(getTitle());
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(getResources().getString(R.string.drawer_open));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
我的XML操作栏:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/menu_search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom"
android:title="Search"
android:visible="false"
android:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="@+id/menu_addition"
android:icon="@drawable/addition_icon"
app:showAsAction="ifRoom"
android:title="Order"
android:visible="false"/>
<item
android:id="@+id/menu_validate"
app:showAsAction="ifRoom"
android:title="Terminer"
android:visible="false"/>
<item
android:id="@+id/menu_update"
app:showAsAction="ifRoom"
android:title="Modifier"
android:visible="false"/>
</menu>
你有什么想法吗? THX
编辑我有一个片段,其中包含textview和edittext上的操作,但我不认为可能是这样。
答案 0 :(得分:1)
好的,我找到了解决方案。
我必须扩展ActionBarActivity而不是FragmentActivity !!!!