本文介绍了Button btn = new Button(this); "this"的用途是什么?在这种情况下....?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如果我们不将"this"作为对Button的上下文引用,将会发生什么…….

What will happen if we don't pass "this" as context reference to the Button....??

推荐答案

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

Context is interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

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

Button is also resource of application so if you want to create button or any widget then you have to pass Context in constructor of widget.

更多详情

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

这篇关于Button btn = new Button(this); "this"的用途是什么?在这种情况下....?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 21:38