亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按鈕 onClick 未綁定到 Activity 中的方法

按鈕 onClick 未綁定到 Activity 中的方法

C#
aluckdog 2022-10-15 14:36:37
因此,我正在嘗試測試將單擊事件添加到我主要使用Button的 .axml 文件中的a 。Activity我經歷了很多其他線程,并且盡我所能遵循提供的代碼。但是,當我運行代碼時出現以下異常。Java.Lang.IllegalStateException: Could not find method testClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'login'在 Main.axml  <Button      android:text="Login"      android:layout_width="match_parent"      android:layout_height="0dp"      android:layout_weight="2"      android:onClick="testClick"      android:id="@+id/login" />在 Main.cspublic class Main : Activity{    protected override void OnCreate(Android.OS.Bundle savedInstanceState)    {        base.OnCreate(savedInstanceState);        SetContentView(Resource.Layout.Main);    }    public void testClick(View view)    {        //Show alert saying clicked...    }}我是否缺少一些配置,或者包括讓它工作?
查看完整描述

2 回答

?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

你總是可以這樣做:


Button button1 = FindViewById<Button>(Resource.Id. login);

button1.Click += delegate => {

 //Show alert saying clicked...

};

如果您不想使用這種方式,則應Export首先使用您的方法,如下所示查看此答案:


[Export("testClick")]

public void testClick(View view)

{

    //Show alert saying clicked...

}

如果這些不起作用,請添加參考Mono.Android.Export.dll,然后在你的Activity放這個


[Java.Interop.Export("testClick")]

public void testClick(View view)

{

    //Show alert saying clicked...

}


查看完整回答
反對 回復 2022-10-15
?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

請在繼承接口 View.IOnclicklistener 后嘗試:


public class Main: AppcompatActivity, View.IonClickListener

它有助于像您給出的那樣實現界面的 onclick 方法:


public void testClick(View view)

    {

        //Show alert saying clicked...

    }


查看完整回答
反對 回復 2022-10-15
  • 2 回答
  • 0 關注
  • 195 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號