SoFunction
Updated on 2025-04-11

Four ways to implement monitoring on Android to explain the example code in detail

Directly upload the code, you can refer to it

(1) It is the event listener

package .s07150745.work5;

import .;
import ;
import ;
import ;
import ;

public class MainActivity extends AppCompatActivity implements {

@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
Button btn1 = (Button) findViewById(.btn1);
("Click");
(this);
}

@Override
public void onClick(View v) {
(this,"Clicked to me...",Toast.LENGTH_SHORT).show();
("Clicked to me...");
}
}

 

(2) External class acts as event listeners:

package .s07150745.work5;

import .;
import ;
import ;
import ;

public class Act2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_act2);
Button btn2 = (Button) findViewById(.btn2);
("Click");
(new OuterClass("Clicked to me..."));
}
}
class OuterClass implements {
private String str="Clicked me...";
public OuterClass(String str){
super();
=str;
}
@Override
public void onClick(View v) {
(str);
}
}

(3) Internal class serves as event listeners:

package .s07150745.work5;

import .;
import ;
import ;
import ;
import ;

public class Act3 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_act3);
Button btn3 = (Button) findViewById(.btn3);
("Click");
(new OuterClass());
}
class OuterClass implements {

@Override
public void onClick(View v) {
(,"Clicked to me...",Toast.LENGTH_SHORT).show();
("Clicked to me...");
}
}
}

(4) Anonymous class serves as event listeners:

package .s07150745.work5;

import .;
import ;
import ;
import ;
import ;

public class Act4 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_act4);
Button btn4 = (Button) findViewById(.btn4);
("Click");
(new () {
@Override
public void onClick(View v) {
(,"Clicked to me...",Toast.LENGTH_SHORT).show();
("Clicked to me...");
}
});
}
}

 

After the above four methods, define an extra Android label in each event, that is, activity, in the xml file. Don't forget to add the following blue code to each activity.

<activity android:name=".MainActivity" android:label="1">
<intent-filter>
<action android:name="" />

<category android:name="" />
</intent-filter>
</activity>

Thank you for reading, I hope it can help you. Thank you for your support for this site!