我想创建一个意图,一旦单击菜单项,便会启动新活动,但是我不确定如何执行此操作。我一直在阅读android文档,但是我的实现是不正确的..一些正确方向的指导会有所帮助。我在下面列出了我的代码并注释了我的问题领域,我认为我在调用错误的方法。
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);
}
}