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.