我正在尝试创建一个选项菜单,它会显示菜单,但当我单击任何东西时,它都会显示我的代码在下面的Toast.makeText文本。但是当我把代码放在同一个活动中时,它会显示文本。
package com.officextracts.kaspersky;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class Option_menu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option_menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.option_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_home:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(this, "Home Is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_krp:
Toast.makeText(this, "Kaspersky Retail Products", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_kep:
Toast.makeText(this, "Kaspersky Endpoint Products", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_fkr:
Toast.makeText(this, "Find Kaspersky Resaller", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_sales:
Toast.makeText(this, "Contact Kaspersky Sales", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_crs:
Toast.makeText(this, "Contact Retail Support", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_ces:
Toast.makeText(this, "Contact Enterprise Support", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_coo:
Toast.makeText(this, "Contact Our Office", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_sms:
Toast.makeText(this, "SMS for Support", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_email:
Toast.makeText(this, "Email Support", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_exit:
finish();
System.exit(0);
default:
return super.onOptionsItemSelected(item);
}
}
}现在,你可以看到菜单底部有一个退出按钮,当我从其他活动中调用它时,它确实起作用了,但在同一个活动中,它确实起作用了。
我是android新手,请详细解释一下..
感谢你
发布于 2013-09-08 18:30:12
在您的第一个Activity中,我们将其命名为A,您将启动名为Options_menu的Activity。
使用startActivityForResult(Intent, int)而不是startActivity(Intent)启动int是一个requestCode。这应该是一个final static int,我们称它为yourRequestCodeInt,然后在A中重写onActivityResult(),if(requestCode == yourRequestCodeInt)将你的数据从意图中取出,这是你必须在Toast中显示的消息。
在Options_menu中,在完成activity和seResult(Result.OK)或类似的操作之前,将想要显示的字符串放在intent中
https://stackoverflow.com/questions/18682615
复制相似问题