2011-12-16 6 views
2

私の最初のMonoDroidアプリケーションでは、TextViewをサブクラス化して各ビュー(Android - Way to appear bordered text on the TextView?)の周りに境界線を表示し、それをレイアウトXML(Declaring a custom android UI element using XML)に追加することができ、MonoDroidのAndroidネームスペースの小文字のためNullReferenceExceptions )。私は今、何をしようとしているアクティビティ内のすべてのインスタンスをインスタンス化せずにサブクラス化されたビューイベントを処理するにはどうすればよいですか?

enter image description here

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

enter image description hereenter 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(); 
} 

はBorderedTextViewsの数が変化することが起こっているので、私は活動のOnCreateイベントの各ビューをインスタンス化することなく、クリックイベントを処理したいと思います。私は、アンドロイド:clickableとandroid:onClick属性を使って、レイアウトXMLファイルで何かを行うことができると考えました。

<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"/> 

しかし、それはこの方法(Mono Droid onClick event not found)で登録したイベントをサポートしていませんMonoDroidが判明。

私は現在、XamarinのAPI DesignページのEvents and Listenersセクションの情報を使用して、SetOnClickListenerとビューのOnTouchEventイベントを実験しました。

私が望むのは、BorderedTextViewクラスの単一のメソッドですべてのBorderedTextViewクリックイベントを処理する方法です。アクティビティのOnCreate内の各ビューをインスタンス化する必要はありません。これはMonoDroidで可能ですか、単にこの時点でツールがサポートしないことをやろうとしています。

ありがとうございます。

アップデート - BorderedTextViewのコンストラクタにイベントハンドラをフックする12.16.11

jpobstの提案が働いていました。

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

答えて

3

はあなただけBorderedTextViewコンストラクタでイベントハンドラをフックすることはできませんクリックを処理するための実際の方法は?

+0

ありがとう、それは働いていました(私が木の森を見ず、必要以上に複雑なものになってしまった場合)。解決策で質問を更新しました。 – billmaya

関連する問題