【问题标题】:Using MonoDroid is there a way to do a DoClick() switch / case statement with buttons?使用 MonoDroid 有没有办法用按钮做一个 DoClick() 开关/案例语句?
【发布时间】:2012-09-02 11:38:09
【问题描述】:

我只是想将我在原生 Android/Java 中编写的一些代码移植到 MonoDroid - 但是当我单击按钮时出现以下错误:

java.lang.IllegalStateException:在 id 为“createProfilePicBtn”的视图类 android.widget.Button 上的 onClick 处理程序的活动类 icantalk.android.CreateProfile 中找不到方法 DoClick(View)

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

【问题讨论】:

    标签: android button xamarin.android switch-statement


    【解决方案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;
           }
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 2021-05-09
    • 2020-07-16
    相关资源
    最近更新 更多