【问题标题】:How can you handle subclassed view events without instantiating every instance in the activity?如何在不实例化活动中的每个实例的情况下处理子类视图事件?
【发布时间】:2012-01-09 17:20:42
【问题描述】:

在我的第一个 MonoDroid 应用程序中,我对 TextView 进行了子类化,因此我可以在每个视图周围显示一个边框 (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)。

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

我知道我可以使用 FindViewById 获取每个视图并创建一个委托来处理每个视图的 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 ();
};

更进一步,我可以在 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();
}

由于 BorderedTextViews 的数量会有所不同,我想处理点击事件,而不必在活动的 OnCreate 中实例化每个视图。我想我可以在布局 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 和视图的 OnTouchEvent 事件,但没有成功(使用 Xamarin API Design 页面上的事件和侦听器部分的信息)。

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

提前致谢。

更新 - 12.16.11

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();
}

【问题讨论】:

    标签: event-handling xamarin.android subclass


    【解决方案1】:

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

    【讨论】:

    • 谢谢,这很有效(我没有只见树木不见森林,这让事情变得比他们需要的更复杂)。已更新问题和解决方案。
    猜你喜欢
    • 1970-01-01
    • 2014-06-18
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多