我正在尝试从标题栏中的按钮实现ContextMenu,但它不会出现 工作。我注册了按钮,但是当我点击它时,什么都没有 正在发生。有任何想法吗?我正在使用MonoDroid 1.2。感谢。
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.main);
Button btnMenu =
(Button)FindViewById(Resource.Id.btn_menu_options);
RegisterForContextMenu(btnMenu);
}
public override void OnCreateContextMenu(IContextMenu menu, View v,
IContextMenuContextMenuInfo menuInfo)
{
base.OnCreateContextMenu(menu, v, menuInfo);
menu.SetHeaderTitle(Resource.String.menu_title);
menu.Add(Resource.String.menu_option1);
}
}
答案 0 :(得分:1)
“标题栏中的按钮”是什么意思?我刚刚创建了演示项目,一切正常。这是一个代码:
[Activity(Label = "MonoAndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var button = FindViewById<Button>(Resource.Id.MyButton);
RegisterForContextMenu(button);
}
public override void OnCreateContextMenu(IContextMenu menu, View v, IContextMenuContextMenuInfo menuInfo)
{
base.OnCreateContextMenu(menu, v, menuInfo);
menu.SetHeaderTitle(Resource.String.MenuTitle);
menu.Add(Resource.String.Action1);
menu.Add(Resource.String.Action2);
}
public override bool OnContextItemSelected(IMenuItem item)
{
Toast.MakeText(this, item.TitleFormatted, ToastLength.Short).Show();
return true;
}
}
答案 1 :(得分:0)
我相信上下文菜单是长按。只需按下按钮就不会激活它。你需要长按。