android - 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);
};
}
解决方案
好像您正在使用 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);
};
这将起作用。
推荐阅读
- aurelia - 为 aurelia-slickgrid 导入 excel-export
- javascript - useState 不适用于材料 ui 功能组件(firebase)
- php - 在 laravel 的引导模式中动态显示数据
- python - 如何在 discord.py cogs 中使用 ban_error 和 kick_error?
- reactjs - 限制孩子的类型
- heroku - 如何在 heroku 上启用 gatsbyjs 客户端专用路由
- twilio - 通过 SIP 将物理 IP 电话与 Twilio 链接
- c++ - 链接两个文件
- javascript - 外部 Javascript 不起作用,它被 Atom 显示为灰色。它们在同一个文件夹中,它只是 Atom 还是我只是愚蠢?
- c# - 在这种情况下不允许异步操作