Activity

In the Activity, bind the controls in the layout using the findViewById(int resId) methodCopy the code

View Common event interface

View event monitoring refers to the interaction between users and applications. When users click, long press, touch, move and other actions on the View, the program will process these actions

OnClickListener Listener when you click the View OnLongClickListener Listener when you hold down the View OnTouchListener Listener when you touch the ViewCopy the code

1. In Android, OnClickListener is an interface that handles click events

Public void onClick(View v){// This method is triggered when the control is pressed for a long time. // When multiple components share the same listener, it is used to distinguish components}Copy the code

The OnLongClickListener interface is basically the same as the OnLongClickListener interface, except that this interface is the interface to capture the View long press event. That is, the event triggered when a View is pressed for a long time. The corresponding callback method of the interface is as follows:

Public void onLongClick(View v){public void onLongClick(View v){public void onLongClick(View v){ The return value of this method is a Boolean variable. // When true, the event has been processed completely and no other callback methods are expected to process it again. // When false, the event has not been processed and other methods are expected to continue processing it. }Copy the code

Here are some things to keep in mind:

  • A listener is an interface that contains a function that the system calls when an event is triggered
  • In the implementation class, override this function based on your project
  • The listener needs to be attached to the button, just like an earphone that emits sound, but you can’t hear it without wearing it. Typically, one View may need one listener, and another View may need another. Each listener does its job, but it is possible to bind multiple buttons to a listener if the function is similar.
  • All kinds of controls have common events. The binding Listener function is named setOn ** Listener

Several ways to set up a listener

1) Let the Activity implement the interface

public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button back = (Button) findViewById(R.id.back); back.setOnClickListener(this); } @override public void onClick(View v) {Copy the code

2) Anonymous inner classes

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button back = (Button) findViewById(R.id.back); Back.setonclicklistener (new OnClickListener() {@override public void onClick(View v) {back.setonClickListener (new OnClickListener() {@override public void onClick(View v) { }}Copy the code

3) onClick can be set in XML

<Button android:id="@+id/back" android:onClick="buttonClick" android:layout_width="match_parent" Android :layout_height="wrap_content" Android :layout_weight="1" Android :text="←" /> // Then create the click event method in your code // it must be public void OnClick (View v) public void buttonClick(View v){Copy the code

4) implementation class

Public class MyClick implements OnClickListener {@override public void onClick(View v) {// click events}} @override protected  void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button back = (Button) findViewById(R.id.back); MyClick listener = new MyClick(); back.setOnClickListener(listener); }Copy the code

5) Member variables

Private OnClickListener Listener = new OnClickListener() {@override public void onClick(View v) {// Click event}}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button back = (Button) findViewById(R.id.back); back.setOnClickListener(listener); }Copy the code

Share with you

I want to work my way up

Fly forward on the blades at the highest point

Let the wind blow dry tears and sweat

I want to work my way up

Waiting for the sun to watch its face

Little days have big dreams

I have my day

Let the wind blow dry tears and sweat

One day I will have my day