首页 > 解决方案 > Visual Studio:资源.ID。找不到我的按钮

问题描述

我正在尝试创建一个将我带到新页面的按钮。其他任何方法都可以,只是Resource.ID找不到按钮的 ID。在另一个程序中,它工作正常,但我不知道我做错了什么。这是我的按钮:

<Button
    android:text="Next Page"
    android:layout_width="150dp"
    android:layout_height="120dp"
    android:layout_marginTop="150dp"
    android:layout_marginLeft="100dp"
    android:id="@+id/Button1"
    android:background="#fff55858"
    android:drawableLeft= "@drawable/abc_ic_star_black_36dp"/>

这就是我的cs页面:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    SetContentView(Resource.Layout.activity_main);

    var btn_nextPage = FindViewById<Button>(Resource.ID.Button1);
    btn_nextPage.Click += (s, e) =>
    {
        Intent nextActivity = new Intent(this, typeof(layout1));
        StartActivity(nextActivity);
    };

}

标签: androidvisual-studio

解决方案


好像您正在使用 Kotlin。

使用 kotlin 时,您无需创建任何视图的实例即可使用它。您可以直接使用resource id,如下所示:

// If, 'Button1' is the id of your Button, then use this:
Button1.Click += (s, e) => {
    Intent nextActivity = new Intent(this, typeof(layout1));
    StartActivity(nextActivity);
};

这将起作用。


推荐阅读