2012-09-02 37 views
0

我只是想端口一些代碼我在原生Android/Java的寫信給MonoDroid的 - 但我得到當我點擊一個按鈕,出現以下錯誤:使用MonoDroid有沒有辦法用按鈕做DoClick()switch/case語句?

java.lang.IllegalStateException:找不到的方法DoClick(查看)在活動課icantalk.android.CreateProfile爲的onClick處理程序上的視圖類android.widget.Button ID爲 'createProfilePicBtn'

public void DoClick(View view) 
    { 
     switch (view.Id) 
     { 
      case Resource.Id.createProfilePicBtn: 
       { 
        Log.Error("Profile Pic", "Clicked"); 
        break; 
       } 
      case Resource.Id.createProfileSbmtBtn: 
       { 
        Log.Error("Save Button", "Clicked"); 
        break; 
       } 
     } 
    } 

我的佈局XML的相關部分:

 <Button 
     android:id="@+id/createProfilePicBtn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:layout_marginTop="10dip" 
     android:onClick="DoClick" 
     android:text="@string/createProfileImgBtnTxt" /> 

     <Button 
     android:id="@+id/createProfileSbmtBtn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:layout_marginTop="10dip" 
     android:onClick="DoClick" 
     android:text="@string/createProfileSaveBtnTxt" /> 

回答

1

MonoDroid目前不支持以這種方式註冊事件。

可以註冊事件中使用代碼:

public override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    //Do other oncreate stuff here (including SetContentView etc) 

    //Register the event for your first button 
    Button btn = FindViewById<Button>(Resource.id.createProfilePicBtn); 
    btn.Click += DoClick; 

    //Register the event for your second button 
    Button btn2 = FindViewById<Button>(Resource.id.createProfileSbmtBtn); 
    btn2.Click += DoClick; 
} 


public void DoClick(object sender, EventArgs e) 
{ 
    View view = (View)sender; 
    switch (view.Id) 
    { 
     case Resource.Id.createProfilePicBtn: 
     { 
      Log.Error("Profile Pic", "Clicked"); 
      break; 
     } 
     case Resource.Id.createProfileSbmtBtn: 
     { 
      Log.Error("Save Button", "Clicked"); 
      break; 
     } 
    } 
} 
+0

尼斯和清潔。謝謝! – Kiada

+0

Mono for Android 4.2值得注意,您現在可以導出「DoClick」方法,以允許從Axml調用此方法(請參閱http://docs.xamarin.com/android/Releases/Mono_For_Android_4/Mono_For_Android_4.2#有關更多信息,請參閱Exporting_arbitrary_Java_member_for_better_Java_integration) – chrisntr

相關問題