有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

调用了java onCreateOptionMenu(),但不起作用

我对onCreateOptionMenu()方法有问题。它不起作用

当我的活动有焦点时,我想显示按钮。但是什么也没发生

我真的不明白

个人活动:

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 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto" >

    <item
        安卓:id="@+id/menu_search"
        安卓:icon="@drawable/ic_action_search"
        app:showAsAction="ifRoom"
        安卓:title="Search"
        安卓:visible="false"
        安卓:actionViewClass="安卓.support.v7.widget.SearchView" />
    <item
        安卓:id="@+id/menu_addition"
        安卓:icon="@drawable/addition_icon"
        app:showAsAction="ifRoom"
        安卓:title="Order"
        安卓:visible="false"/>
    <item
        安卓:id="@+id/menu_validate"
        app:showAsAction="ifRoom"
        安卓:title="Terminer"
        安卓:visible="false"/>

    <item
        安卓:id="@+id/menu_update"
        app:showAsAction="ifRoom"
        安卓:title="Modifier"
        安卓:visible="false"/>

</menu>

你有什么想法吗?Thx

EDIT我有一个关于textview和edittext操作的片段,但我认为它不可能是这样


共 (1) 个答案

  1. # 1 楼答案

    好的,我找到了解决办法

    我必须扩展ActionBarActivity,而不是FragmentActivity