【发布时间】:2014-09-10 12:36:50
【问题描述】:
这个问题应该很简单(我认为),但我不知道如何正确地做。我对编程还是很陌生。
我有一个继承自 EditText 的简单类。有两个问题:
- 最初,Touch 事件触发 3 次,第二次触摸将触发该事件 5 次,依此类推。这是怎么回事?
- 我不知道如何取消订阅活动,也不知道在什么阶段。通常,在处理活动时,我只需在 OnResume 期间订阅并在 OnPause 期间取消订阅。在这种情况下,正确的解决方案是什么?我应该编写一个自定义方法并从父类调用它吗?我应该实现 IDisposable 还是类似的东西?我应该以不同的方式编写课程吗?
这是我的自定义编辑文本演示类:
namespace HelloWorld_Android {
class DemoEditText : EditText {
public DemoEditText (Context context, IAttributeSet attrs) : base(context, attrs) {
this.Touch += HandleTouch;
}
void HandleTouch (object sender, TouchEventArgs e) {
Console.WriteLine ("Fired");
}
}
}
活动:
namespace HelloWorld_Android {
[Activity (Label = "HelloWorld_Android", MainLauncher = true)]
public class MainActivity : Activity {
protected override void OnCreate (Bundle bundle) {
base.OnCreate (bundle);
SetContentView(Resource.Layout.Main);
}
}
}
我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<HelloWorld_Android.DemoEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/test"
/>
</LinearLayout>
【问题讨论】:
标签: android events xamarin.android unsubscribe