Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

Handling a Menu Item Click Event - Android

I want to create an intent that starts a new activity once a Menu Item is clicked, but I'm not sure how to do this. I've been reading through the android documentation, but my implementation isn't correct..and some guidance in the right direction would help. I've listed my code below and commented out my problem areas, I think I'm invoking the wrong method.

package com.jbsoft.SimpleFlashlight;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SimpleFlashLightActivity extends Activity {


  Button GreenButton;   // Declare instances of buttons to use later
  Button BlueButton;

  private static final int OK_MENU_ITEM = Menu.FIRST;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    BlueButton = (Button) findViewById(R.id.bluebutton);
    BlueButton.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {

        //Display msg when user clicks Blue Button
        showColorChangeMsg();

        // Switch Activities on click
        Intent blueintent = new Intent(SimpleFlashLightActivity.this,
                                       BlueFlashLightActivity.class);
        startActivity(blueintent);

      }
    });
    //Install listener for second button
    GreenButton = (Button) findViewById(R.id.greenbutton);
    GreenButton.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {

        // Display msg when user clicks Green Button
        showColorChangeMsg();

        Intent greenintent = new        Intent(SimpleFlashLightActivity.this,
                                               GreenFlashLightActivty.class);
        startActivity(greenintent);

      }
    });

    ;

    /**************************************************************************************/

    // Method Declarations // THIS IS WHERE I'M HAVING A PROBLEM

    MenuItem AddColorButton = (MenuItem)findViewById(R.id.menu_insert);

    boolean onOptionsItemSelected(AddColorButton) {
      Intent intent = new  Intent(SimpleFlashLightActivity.this,
                                  BlueFlashLightActivity.class);
      startActivity(intent);
      return true;
      ;
    };
    /****************************************************************************************/

  }
  private void showColorChangeMsg()
  {
    Toast msgtoast = Toast.makeText(this.getBaseContext(), "SWITCH COLOR!",
                                    Toast.LENGTH_LONG);
    msgtoast.show();
  }
  private void showMsg(String msg) {
    Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
    toast.show();
  }

  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.list_menu, menu);
    return true;

  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case OK_MENU_ITEM:
      showMsg("OK");
      break;
    }
    return super.onOptionsItemSelected(item);
  }

}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simple code for creating menu.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

Simple code for menu item selection

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.new_game:
        newGame();
        return true;
    case R.id.help:
        showHelp();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

You can find more detail from below link..

Menus

Menu resource


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...