Android ActionBar Sherlock 菜单项 OnClick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10472443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:51:49  来源:igfitidea点击:

ActionBar Sherlock Menu Item OnClick

androidmenuclickactionbarsherlock

提问by G'sson

I am new to using the Sherlock ActionBar and I have make it run in my app and I have a item in the actionbar to but I don't know how to make the item do something when it's clicked all I got is this.

我是使用 Sherlock ActionBar 的新手,我已经让它在我的应用程序中运行,我在操作栏中有一个项目,但我不知道如何让项目在被点击时做一些事情,我得到的就是这个。

public boolean onCreateOptionsMenu(Menu menu) {

    menu.add("Folder")
        .setIcon(R.drawable.folder)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    return true;
}


public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        //What do i write here?
    return true;

I hope you understand what I mean :)

我希望你明白我的意思:)

EDIT

编辑

Hey I made it work with a little help from this threadthat I found and I made a few changes and here is the code! :DDD

嘿,我在我发现的这个线程的帮助下使它工作了,我做了一些更改,这是代码!:DDD

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
   inflater.inflate(R.menu.menu, menu);
   return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.search:
            finish();
            return true;
        case R.id.new_folder:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

回答by Hema

Try this, it works:

试试这个,它有效:

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
      com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
      inflater.inflate(R.layout.menu, menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
      // Handle item selection
      switch (item.getItemId()) {
      case R.id.settings:
          Intent i=new Intent(class1.this, clas2.class);
          startActivity(i);
          return true;
      }
      return false;
}

回答by ernell

Something like this (taken from one of my apps). The activity in this case extends SherlockFragmentActivity.

像这样的东西(取自我的一个应用程序)。 本例中的活动扩展了SherlockFragmentActivity.

    @Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    menu.add(Menu.NONE, R.id.ID_ACTION_EXIT, Menu.NONE,R.string.action_label_exit)
    .setIcon(android.R.drawable.ic_menu_close_clear_cancel)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(
        com.actionbarsherlock.view.MenuItem item) {
    switch(item.getItemId()){
    case R.id.ID_ACTION_EXIT:
        Main.this.finish();
        return true;
    default:
        return false;
    }
}

The R.id.ACTION_EXITis placed in res/values/ids.xml

R.id.ACTION_EXIT被放置在res/values/ids.xml

<resources><item name="ID_ACTION_EXIT" type="id"/></resources>

[EDIT] Try this then (extends SherlockListActivity). You have to do some editing first. This code comes from the Sherlock demos. You should download them.

[编辑] 然后试试这个(扩展 SherlockListActivity)。你必须先做一些编辑。此代码来自 Sherlock 演示。你应该下载它们。

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    SubMenu sub = menu.addSubMenu("Theme");
    sub.add(0, R.style.Theme_Sherlock, 0, "Default");
    sub.add(0, R.style.Theme_Sherlock_Light, 0, "Light");
    sub.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0, "Light (Dark Action Bar)");
    sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home || item.getItemId() == 0) {
        return false;
    }
    THEME = item.getItemId();
    Toast.makeText(this, "Theme changed to \"" + item.getTitle() + "\"", Toast.LENGTH_SHORT).show();
    return true;
}

回答by Andre

Please try this too.

请也试试这个。

import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class ActionBarTestActivity extends SherlockActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_bar_test);
    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {

        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.activity_action_bar_test, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch (item.getItemId()) {
        case R.id.phone:
            Toast.makeText(getBaseContext(), "You selected Phone",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.computer:
            Toast.makeText(getBaseContext(), "You selected Computer",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.gamepad:
            Toast.makeText(getBaseContext(), "You selected Gamepad",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.camera:
            Toast.makeText(getBaseContext(), "You selected Camera",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.video:
            Toast.makeText(getBaseContext(), "You selected Video",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.email:
            Toast.makeText(getBaseContext(), "You selected EMail",
                    Toast.LENGTH_SHORT).show();
            break;

        }
        return true;
    }
}

回答by Satheesh

You use this one

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
      getSherlockActivity().getSupportMenuInflater().inflate(R.menu.settingmenu, menu);
    getSherlockActivity().getSupportMenuInflater().inflate(R.menu.menugridcalendar,me
}

This will be added in your sherlockfragment activity:


public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.settings:
          Intent intent=new Intent(currentclass.this, calledclass.class);
          startActivity(intent);
          return true;
      }
      return false;
}