如何在 MonoDroid 中使用长按?

发布于 2024-12-06 10:13:14 字数 439 浏览 0 评论 0原文

目前我正在以这种方式使用长按:

button.SetOnLongClickListener(new MyLongClickListener());

public class MyLongClickListener : View.IOnLongClickListener
{
    public bool OnLongClick(View v)
    {
        //do something pretty cool
        return true;
    }

    public IntPtr Handle
    {
        get { throw new NotImplementedException(); }
    }
}

但是编写一个类只是为了在 OnLongClick 方法中执行简单的一行或两行似乎不是很聪明。所以我想知道是否有更好的解决方案?

in the moment i'm using long-clicks this way:

button.SetOnLongClickListener(new MyLongClickListener());

public class MyLongClickListener : View.IOnLongClickListener
{
    public bool OnLongClick(View v)
    {
        //do something pretty cool
        return true;
    }

    public IntPtr Handle
    {
        get { throw new NotImplementedException(); }
    }
}

But writing a class just to do an easy one or two-liner in the OnLongClick-method seems not to be very smart. So i'm wondering if there is a better solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还如梦归 2024-12-13 10:13:14

编写侦听器类的方法就是在 Java 中执行此操作的方法,这就是为什么它在 Android 的 Mono 中如此公开的原因。也就是说,在 Mono for Android 中,您可以分配 LongClickHandlerLongClick 属性(如果您愿意)。如果你愿意的话。

view.LongClick = onLongClick;

private bool onLongClick(View view) 
{
    // do some stuff

    return true;
}

或者

view.LongClick = (clickedView) =>
{
    // do some stuff

    return true;
};

The approach of writing a listener class is the way to do it in Java, which is why it's exposed as such in Mono for Android. That said, in Mono for Android you can assign a delegate of type LongClickHandler to the LongClick property if you'd prefer that. if you'd prefer that.

view.LongClick = onLongClick;

private bool onLongClick(View view) 
{
    // do some stuff

    return true;
}

or

view.LongClick = (clickedView) =>
{
    // do some stuff

    return true;
};
墨落画卷 2024-12-13 10:13:14

请参阅此示例代码:

[Activity(Label = "My Activity", MainLauncher = true)]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);            
    SetContentView(Resource.layout.main);

    Button button = FindViewById<Button>(Resource.id.button);
    TextView view = FindViewById<TextView>(Resource.id.text);

    button.Click += (s, args) => view.Text = "Clicked!";
    button.LongClick += (s, args) =>
                            {
                                view.Text = "Long click!";
                                args.ReturnValue = false;
                            };
}
}

See this example code:

[Activity(Label = "My Activity", MainLauncher = true)]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);            
    SetContentView(Resource.layout.main);

    Button button = FindViewById<Button>(Resource.id.button);
    TextView view = FindViewById<TextView>(Resource.id.text);

    button.Click += (s, args) => view.Text = "Clicked!";
    button.LongClick += (s, args) =>
                            {
                                view.Text = "Long click!";
                                args.ReturnValue = false;
                            };
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文