gpt4 book ai didi

android - Button btn = new Button(this); "this"在这种情况下有什么用......?

转载 作者:行者123 更新时间:2023-11-29 02:11:39 25 4
gpt4 key购买 nike

Button btn = new Button(this);
btn.setText(“This is a Button”);
btn.setLayoutParams(params);

如果我们不将“this”作为上下文引用传递给按钮,会发生什么......??

最佳答案

上下文 是有关应用程序环境的全局信息的接口(interface)。这是一个抽象类,其实现由Android系统提供。它允许访问特定于应用程序的资源和类,以及对应用程序级操作的向上调用,例如启动 Activity 、广播和接收 Intent 等。

按钮也是应用程序的资源,因此如果您想创建按钮或任何小部件,则必须在小部件的构造函数中传递上下文。

了解更多

http://developer.android.com/reference/android/content/Context.html

关于android - Button btn = new Button(this); "this"在这种情况下有什么用......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6922765/

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