ActionBar Sherlock Menu Item OnClick

前端 未结 4 1562
臣服心动
臣服心动 2021-02-04 03:16

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 cl

4条回答
  •  悲哀的现实
    2021-02-04 03:43

    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;
        }
    }
    

提交回复
热议问题