gpt4 book ai didi

android - Visual Studio/Xamarin OnClickListener

转载 作者:行者123 更新时间:2023-11-30 01:22:47 25 4
gpt4 key购买 nike

我是编程新手,如果这是一个愚蠢的问题,我深表歉意!我正在 VS15/Xamarin 中构建一个应用程序,并试图设置一个 onClickListener,但它一直告诉我有一个错误“类型名称‘OnClickListener’在‘View’类型中不存在”。我尝试了多种解决方案,但显然我遗漏了一些东西!这是我的代码:

    using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace MyStory
{
[Activity(Label = "MyStory", MainLauncher = true, Icon = "@drawable/Books")]
public class MainActivity : Activity
{


protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

SetContentView(Resource.Layout.Main);

Button btn_Short = FindViewById<Button>(Resource.Id.btn_Short);
Button btn_Flash = FindViewById<Button>(Resource.Id.btn_Flash);
Button btn_Poet = FindViewById<Button>(Resource.Id.btn_Poet);
Button btn_About = FindViewById<Button>(Resource.Id.btn_About);

btn_About.SetOnClickListener(new View.OnclickListener())
{
@override
public void onClick(View v)
{
startActivity(new Intent(MainActivity.this, About.class));
}
}
}
}
}

这是屏幕的样子:

screenshot

最佳答案

C# 不是 java。试试这样的东西:

btn_About.Click += myCustomClick;

然后在你的 oncreate 之外:

public void myCustomClick(object o, EventArgs e) {
//handle click here
}

但是检查语法。

如果你想要它,你应该让你的 Activity 像这样实现 View.IOnClickListener:

public class MainActivity: Activity, View.IOnClickListener
{
//code here
}

关于android - Visual Studio/Xamarin OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36855674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com