关于Android Studio和按钮在主要活动中不同步的新手问题 [英] Newbie question about Android Studio and button not syncing in main activity

查看:85
本文介绍了关于Android Studio和按钮在主要活动中不同步的新手问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是教程

https://www.youtube.com/watch?time_continue= 289& v = OGfZpfn-dGI

在我的android studio中,它无法识别按钮

In my android studio it doesn't recognize the button iv'e name them

android:id ="@ + id/top_button" android:text =顶部按钮" android:text =按钮2" android:android:id ="@ + id/button_2"

top_button.setOnClicklistner {println(单击顶部按钮) Button_2.setOnClicklistner {println(按钮)

推荐答案

我们在这里缺少很多上下文,可能会对我们有所帮助.

We're missing a lot of context here that would probably help us help you.

首先要注意以下几点:-XML布局中的 android:id 属性是如何命名相关视图.这通常是您将在代码中引用视图"的方式.- android:text 属性是用户在诸如TextView之类的视图上可见的文本.-为了使 top_button 引用XML布局文件中的所需视图,需要将其绑定在代码中.有两种正常的方法 findViewById()和数据绑定.

A few things first though: - The android:id property in your XML layout is how you name the View in question. This is most often how you will reference the View in code. - The android:text property is the user visible text on views like TextView. - In order for top_button to refer to your desired View in your XML layout file, it needs to be bound in code. There's a couple of normal ways of doing it findViewById() and data-binding.

现在,我要假设最后一步是您所缺少的(这似乎是此时最有可能的罪魁祸首)...以下是绑定它的几种方法:

I'm going to assume, for now, that the last step is what you are missing (it seems the most likely culprit at this point)... Here's a few ways to bind it:

方法1:使用Activity类时

如果您要从 Activity 类将 top_button 绑定到视图,则该方法应该起作用:

If you're binding top_button to your View from an Activity class, this should work:

private lateinit var top_button // replace View here with your widget's type

fun onCreate(...) {
    super.onCreate(...)
    setContentView(R.layout.<i>your_layout_file_name_here</i>)
    top_button = findViewById(R.id.top_button)
    ...
}

方法2:使用Fragment类时

如果要从 Fragment 类将 top_button 绑定到视图,则更像这样:

If you're binding top_button to your View from a Fragment class, it's more like this:

private lateinit var top_button: View // replace View here with your widget's type

fun onCreateView(...): View {
    val rootView = layoutInflater.inflate(R.layout.<i>your_layout_file_name_here</i>)
    top_button = rootView.findViewById(R.id.top_button)
    ...
    return rootView
}

方法3:使用数据绑定时

我本人更喜欢这种方法,但是您应该Google如何在Android中设置数据绑定,因为您需要在build.gradle以及所有内容中进行更改.

I prefer this method myself, but you should Google how to setup data-binding in Android as you'll need changes in your build.gradle and all.

  • 首先,将XML布局更改为:
<layout>

<!-- your existing layout XML here -->

</layout>

然后在活动/片段"中,假设您的XML布局文件名为 activity_my_cool_screen.xml ,您可以执行以下操作:

Then in your Activity/Fragment, let's say your XML layout file is named activity_my_cool_screen.xml, you can do:

val binding = ActivityMyCoolScreenBinding.inflate(getLayoutInflater())
binding.topButton.setOnClickListener(...)

请注意, ActivityMyCoolScreenBinding 类是为您自动生成的.如果开始时变为红色,则首先验证您已在项目中正确设置了数据绑定,然后再进行操作,请确保导入 ActivityMyCoolScreenBinding 类.如果更改XML布局的文件名,则 ActivityMyCoolScreenBinding 类名称将更改为自动匹配.但是,如果您要更改名称,我建议您使用Android Studio的重构/重命名工具,因为它会搜索您的代码库并在各处进行更新.否则,您必须手动操作(可行,但可能很乏味且容易出错).

Notice here that the ActivityMyCoolScreenBinding class is auto-generated for you. If it turns red at first, then first verify you've accurately setup data-binding in your project, then if that's good to go, make sure to import the ActivityMyCoolScreenBinding class. If you change your XML layout's filename, then the ActivityMyCoolScreenBinding class name will change to match automatically. But, I'd recommend if you do change the name, that you use Android Studio's refactoring/renaming tools as it'll search your codebase and update it everywhere. Otherwise, you have to do it by hand (doable, but potentially tedious and error prone).

祝你好运!

这篇关于关于Android Studio和按钮在主要活动中不同步的新手问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆