Android 使用MonoDroid有没有办法用按钮执行DoClick()switch/case语句?
我只是尝试将我在本机Android/Java中编写的一些代码移植到MonoDroid,但当我单击一个按钮时,会出现以下错误: java.lang.IllegalStateException:在id为“createProfilePicBtn”的视图类android.widget.Button上的onClick处理程序的活动类icantalk.android.CreateProfile中找不到方法DoClick(视图) 我的布局xml的相关部分包括:Android 使用MonoDroid有没有办法用按钮执行DoClick()switch/case语句?,android,button,xamarin.android,switch-statement,Android,Button,Xamarin.android,Switch Statement,我只是尝试将我在本机Android/Java中编写的一些代码移植到MonoDroid,但当我单击一个按钮时,会出现以下错误: java.lang.IllegalStateException:在id为“createProfilePicBtn”的视图类android.widget.Button上的onClick处理程序的活动类icantalk.android.CreateProfile中找不到方法DoClick(视图) 我的布局xml的相关部分包括: MonoDroid目前不支持以这种方式注册事件
MonoDroid目前不支持以这种方式注册事件 您可以使用以下代码注册事件:
public override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//Do other oncreate stuff here (including SetContentView etc)
//Register the event for your first button
Button btn = FindViewById<Button>(Resource.id.createProfilePicBtn);
btn.Click += DoClick;
//Register the event for your second button
Button btn2 = FindViewById<Button>(Resource.id.createProfileSbmtBtn);
btn2.Click += DoClick;
}
public void DoClick(object sender, EventArgs e)
{
View view = (View)sender;
switch (view.Id)
{
case Resource.Id.createProfilePicBtn:
{
Log.Error("Profile Pic", "Clicked");
break;
}
case Resource.Id.createProfileSbmtBtn:
{
Log.Error("Save Button", "Clicked");
break;
}
}
}
public override void OnCreate(Bundle)
{
base.OnCreate(bundle);
//在此处执行其他oncreate操作(包括SetContentView等)
//为您的第一个按钮注册事件
按钮btn=FindViewById(Resource.id.createProfilePicBtn);
btn.Click+=DoClick;
//为第二个按钮注册事件
按钮btn2=FindViewById(Resource.id.CreateProfilesBMTBN);
btn2.点击+=点击文档;
}
public void DoClick(对象发送方,事件参数e)
{
视图=(视图)发送方;
开关(view.Id)
{
案例资源.Id.createProfilePicBtn:
{
日志错误(“配置文件图片”,“点击”);
打破
}
案例资源.Id.createProfileSbmtBtn:
{
日志错误(“保存按钮”、“单击”);
打破
}
}
}
Mono for Android 4.2值得注意的是,您现在可以导出“DoClick”方法,允许从Axml调用它(有关更多信息,请参阅)
public override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//Do other oncreate stuff here (including SetContentView etc)
//Register the event for your first button
Button btn = FindViewById<Button>(Resource.id.createProfilePicBtn);
btn.Click += DoClick;
//Register the event for your second button
Button btn2 = FindViewById<Button>(Resource.id.createProfileSbmtBtn);
btn2.Click += DoClick;
}
public void DoClick(object sender, EventArgs e)
{
View view = (View)sender;
switch (view.Id)
{
case Resource.Id.createProfilePicBtn:
{
Log.Error("Profile Pic", "Clicked");
break;
}
case Resource.Id.createProfileSbmtBtn:
{
Log.Error("Save Button", "Clicked");
break;
}
}
}