Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to add event listeners in Android development

Shulou Source: shulou.com Published: 2022-05-31 13:30:20 10月25日 Update

This article mainly introduces the relevant knowledge of "how to add event listeners in Android development". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to add event listeners in Android development" can help you solve the problem.

First deal with the TRUE button. In the QuizActivity.java file, in onCreate (...) Enter the following code after the variable assignment statement of the method, as shown in the following code.

Set up a listener (QuizActivity.java) for the TRUE button

...

@ Override

Protected void onCreate (Bundle savedInstanceState) {

Super.onCreate (savedInstanceState)

SetContentView (R.layout.activity_quiz)

MTrueButton = (Button) findViewById (R.id.true_button)

MTrueButton.setOnClickListener (new View.OnClickListener () {

@ Override

Public void onClick (View v) {

/ / Does nothing yet, but soon!

}

});

MFalseButton = (Button) findViewById (R.id.false_button)

}

}

(if you encounter an error prompt from View cannot be resolved to a type, use the Option+Return or Alt+Enter shortcut to import the View class.)

In the above code, we set up a listener. When the button mTrueButton is clicked, the listener will notify us immediately. The parameter passed in to the setOnClickListener (OnClickListener) method is a listener. This parameter is an object that implements the OnClickListener interface.

Use anonymous inner classes

The listener parameter passed into the SetOnClickListener (OnClickListener) method is an anonymous inner class (anonymous inner class) implementation, and the syntax looks a little complicated. However, the editor of Nanchang APP production and Development Company tells you a mnemonic tip here: all the implementation code in the outermost parentheses is a parameter passed into the SetOnClickListener (OnClickListener) method. This parameter is the implementation code for a new anonymous inner class.

MTrueButton.setOnClickListener (new View.OnClickListener () {

@ Override

Public void onClick (View v) {

/ / Does nothing yet, but soon!

}

});

All listeners can be implemented as anonymous inner classes, which has two benefits:

First: because of the use of anonymous inner classes, we can implement listener methods in the same place, and the code is more legible.

Second: event listeners are generally only used in the same place, and using anonymous inner classes can avoid unnecessary named class implementations.

The anonymous inner class implements the OnClickListener interface, so it must also implement the unique onClick (View) method of that interface. The code for the onClick (View) method is temporarily an empty structure. Although you need to implement the onClick (View) method to implement the listener interface, it is up to the user to decide how to implement it, so the compiler can compile even if it is an empty implementation.

Refer to the following code to set up a similar event listener for the FALSE button.

Set up a listener (QuizActivity.java) for the FALSE button

...

MTrueButton.setOnClickListener (new View.OnClickListener () {

@ Override

Public void onClick (View v) {

/ / Does nothing yet, but soon!

}

});

MFalseButton = (Button) findViewById (R.id.false_button)

MFalseButton.setOnClickListener (new View.OnClickListener () {

@ Override

Public void onClick (View v) {

/ / Does nothing yet, but soon!

}

});

}

This is the end of the introduction to "how to add event listeners in Android development". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Listeners listeners methods code events parameters buttons development interfaces knowledge that is industry compilation different complex practical two user content variables Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Huawei OPPO Reno Apple Shulou Tech Info