event-handling - 如何在不实例化事件中每个实例的情况下处理子类 View 事件?

标签 event-handling xamarin.android subclass

在我的第一个 MonoDroid 应用程序中,我对 TextView 进行了子类化,以便可以在每个 View 周围显示边框 ( Android - Way to appear bordered text on the TextView? ),设法将其添加到我的布局 XML ( Declaring a custom android UI element using XML ) 中,并避免由于 MonoDroid 的小写而导致的 NullReferenceExceptions Android 命名空间 ( Avoiding a NullReferenceException when creating a custom Android UI element from subclassed TextView )。

enter image description here

我现在想做的是处理每个 BorderedTextView 中的触摸事件。

我知道我可以使用 FindViewById<> 获取每个 View 并创建一个委托(delegate)来处理每个 View 的 Click 事件。

BorderedTextView currentDate = FindViewById<BorderedTextView>(Resource.Id.currentdate);
currentDate.Click += delegate {
    Toast toast = Toast.MakeText(this, "CURRENT DATE tapped", ToastLength.Long);
    toast.Show();
}

BorderedTextView startTime = FindViewById<BorderedTextView>(Resource.Id.starttime);
startTime.Click += delegate {
    Toast toast = Toast.MakeText(this, "START TIME tapped", ToastLength.Long);
    toast.Show ();
};

enter image description here enter image description here

更进一步,我可以在 BorderedTextView 中创建一个通用方法来处理点击(但我仍然需要实例化每个 BorderedTextView)。

// In Activity's OnCreate
BorderedTextView currentDate = FindViewById<BorderedTextView>(Resource.Id.currentdate);
currentDate.Click += delegate {
    BorderedTextView.HandleClicks(this);
}

BorderedTextView startTime = FindViewById<BorderedTextView>(Resource.Id.starttime);
startTime.Click += delegate {
    BorderedTextView.HandleClicks(this);
};

// In BorderedTextView
public static void HandleClicks(Context context)
{
    Toast toast = Toast.MakeText(context, "BorderedTextView tapped", ToastLength.Long);
    toast.Show();
}

由于 BorderedTextView 的数量会有所不同,并且我想处理单击事件,而不必在事件的 OnCreate 中实例化每个 View 。我想我可以在布局 XML 文件中使用 android:clickable 和 android:onClick 属性做一些事情。

<mbta.BorderedTextView
    android:id="@+id/currentdate"
    android:text="CURRENT DATE"
    android:textSize="15pt"
    android:layout_width="fill_parent"
    android:layout_height="75dp"
    android:gravity="center_horizontal|center_vertical"
    android:layout_weight="1"
    android:clickable="true"
    android:onClick="HandleClicks"/>

但事实证明 MonoDroid 不支持以这种方式注册事件 ( Mono Droid onClick event not found )。

我目前已尝试使用 SetOnClickListener 和 View 的 OnTouchEvent 事件,但没有成功(使用 Xamarin API Design 页面上的事件和监听器部分中的信息)。

我想要的是一种使用 BorderedTextView 类中的单个方法来处理每个 BorderedTextView 单击事件的方法,而无需在 Activity 的 OnCreate 中实例化每个 View 。这在 MonoDroid 中可能吗?或者我只是想做一些该工具目前不支持的事情。

提前致谢。

更新 - 2011 年 16 月 12 日

jpobst 关于在 BorderedTextView 的构造函数中连接事件处理程序的建议有效。

public BorderedTextView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
    this.Click += delegate {
        HandleClicks (context);
    };

    this.Tag = this.Text;
}

public BorderedTextView(Context context, IAttributeSet attrs) : base(context, attrs)
{
    this.Click += delegate {
        HandleClicks (context);
    };

    this.Tag = this.Text;
}

public BorderedTextView(Context context) : base(context)
{
    this.Click += delegate {
        HandleClicks(context);
    };

    this.Tag = this.Text;
}

这是处理点击的实际方法

public static void HandleClicks(Context context)
{
    string typeName = ((Type)this.GetType()).Name;
    stirng selected = "Selected " + (string)this.Tag + " (" + typeName + ")";

    Toast.MakeText(context, selected, ToastLength.Short).Show();
}

最佳答案

不能在 BorderedTextView 构造函数中连接事件处理程序吗?

关于event-handling - 如何在不实例化事件中每个实例的情况下处理子类 View 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8535361/

相关文章:

javascript - 注入(inject)的 JS 分配 window.onerror 无法触发

javascript - AngularJS 摘要循环多久运行一次?

xamarin - 新建一个Mobile.App(Xamarin.Forms)项目,尝试编译,但是 "Failed linking references"

cocoa - NSTextField 子类不尊重 -acceptsFirstResponder?

c++ - 当光标到达wxTextCtrl时如何在wxWidgets中创建一个事件

javascript - 迷失在 javascript 范围内

c# - 'Gravity' 不包含 'CENTER_HORIZONTAL' 的定义

c# - 获取响应流时出错 (ReadDone2) : Receive Failure

objective-c - 类方法也被继承了吗?

python - Python 2.7 中的子类