简体   繁体   English

即使在 oncreate 方法中没有调用它们,为什么 android 也会运行像“onItemSelected”这样的方法?

[英]Why do android run methods like "onItemSelected" even if they are not called in the oncreate Method?

Hello everyone i begin to learn Android development and i do not Unterstand how it works.大家好,我开始学习 Android 开发,但我不明白它是如何工作的。 I understood that there is not a main.我明白没有一个主要的。 Instead it is the android lifecyle.相反,它是 android 生命周期。 But now i do not Unterstand why methods like "onItemSelected" or "onTouchEvent" executes by android even if tbis methods do not called.但是现在我不明白为什么即使没有调用 tbis 方法,android 也会执行诸如“onItemSelected”或“onTouchEvent”之类的方法。

Tahir!塔希尔!

These methods are listeners, so they are listening to the special action you make.这些方法是侦听器,因此它们正在侦听您所做的特殊操作。 They will be called every time you select the item or touch a UI stuff you are listening to.每次您选择项目或触摸您正在收听的 UI 内容时,都会调用它们。 For example, there is a method called setOnClickListener which listens every time you click on something, eg any button you have.例如,有一个名为setOnClickListener的方法,它会在您每次单击某些东西(例如您拥有的任何按钮)时进行侦听。 The syntaxis will be like:语法如下:

final Button button = (Button) findViewById(R.id.my_cool_button);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // your handler code here
    }
});

It will be called on touch your button no matter where it is.无论它在哪里,它都会在触摸你的按钮时被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM