public void onPopup(View view)
{
final PopupMenu menu=new PopupMenu(this,view);
menu.getMenuInflater().inflate(R.menu.menu1,menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Toast toast=Toast.makeText(MainActivity.this,item.getTitle()+"Selected",Toast.LENGTH_SHORT);
//Intent intent2 = new Intent(MainActivity.this,YourSpotActivity.class);
//startActivity(intent2);
//startActivity(new Intent(MainActivity.this,YourSpotActivity.class));
toast.show();
return true;
}
});
menu.show();
}
当我单击任何一个列表项时,它将启动另一个活动.@H_502_7@我怎么能通过修改上面的代码来做到这一点.请解释一下.@H_502_7@我在菜单中使用了四种车型.当我选择那辆车中的任何一辆时,它将进入特定的活动.
最佳答案
您需要使用如下所示的开关
原文链接:https://www.f2er.com/android/430916.html switch (item.getItemId()) {
case R.id.menuitem1:
Toast.makeText(getApplicationContext(),"StartActiviy 1",Toast.LENGTH_SHORT).show();
// start activity 1
return true;
case R.id.menuitem2:
Toast.makeText(getApplicationContext(),"StartActiviy 2",Toast.LENGTH_SHORT).show();
// start activity 2
return true;
default:
//default intent
return true;
}
http://developer.android.com/reference/android/widget/PopupMenu.html