gpt4 book ai didi

android - 在哪里设置所有的监听器?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:40:09 25 4
gpt4 key购买 nike

在哪里设置用户界面的所有Listeners
onCreate 中设置它们是好的做法吗?这看起来如此松散和奇怪。
有没有更好的地方设置它们?

最佳答案

来自这里:http://developer.android.com/reference/android/app/Activity.html

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

当您初始化您的 View 时,它们就可以被收听了。 onCreate 是设置监听器的良好回调。换句话说,你可以在 onStartonResume 中设置它,但你应该明白,这是不好的做法,因为 onStartonResume 每次在用户看到您的 Activity 时调用。 onCreate 仅在 Activity 初始化时调用。这就是为什么您应该使用 onCreate 的原因。实际上,好的做法是实现像 initListeners() 这样的方法,您可以在其中放置所有监听器逻辑。

祝你好运!

关于android - 在哪里设置所有的监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21204188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com