Android self-learning mini-question and mini-knowledge point listening events implement listeners multiple times through the interface, android listeners

Source: Internet
Author: User

Android self-learning mini-question and mini-knowledge point listening events implement listeners multiple times through the interface, android listeners
If setOnClickListener (this); implements the same interface when you click the button to implement the listener, you can use the switch () method to perform simple matching without generating so many anonymous internal classes!
Specific steps:
1. initialize the first click imageView = (ImageButton) findViewById (R. id. imageView );
2. Implement the setOnClickListener (this) listener );
3. Implement listening through interfaces
4. initialize the second click imageView2 = (ImageButton) findViewById (R. id. imageView2 );
5. Implement the setOnClickListener (this) listener );
6. Write in the public void Onclick (View v) method:
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. imageView:
Toast. makeText (getApplicationContext (), "you clicked on the robot", Toast. LENGTH_SHORT). show ();
Break;
Case R. id. imageView2:
Toast. makeText (getApplicationContext (), "you clicked the big ear graph", Toast. LENGTH_SHORT). show ();
Break;


Multiple IDs can be matched for the same interface !! Reduces the generation of anonymous internal classes and code bloated



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.