Saya ingin membuat maksud yang memulai aktivitas baru setelah Item Menu diklik, tapi saya tidak yakin bagaimana melakukan ini. Saya telah membaca dokumentasi android, tetapi implementasi saya tidak benar .. dan beberapa panduan ke arah yang benar akan membantu. Saya telah mencantumkan kode saya di bawah ini dan mengomentari area masalah saya, saya pikir saya menggunakan metode yang salah.
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);
}
}