Detailed explanation of several listeners and implementation methods in Android Activity

  • 2021-12-12 05:41:24
  • OfStack

Directory Activity View Common Event Interface Several methods of setting monitoring 1) Enable Activity to implement interfaces 2) Anonymous inner classes 3) onClick can be set in xml 4) Implement classes 5) Member variables

Activity

In Activity, use the findViewById (int resId) method to bind to controls in the layout

View Common Event Interface

Event monitoring of View refers to the interaction between the user and the application. When the user clicks, presses, touches and moves View, the program processes these actions

OnClickListener listening when clicking View
Monitoring when OnLongClickListener presses View for a long time
Monitoring when OnTouchListener touches View

1. In the android system, OnClickListener is an interface for handling click events


public void onClick(View v){
	// Parameter v Is an event source control that triggers the method when the control is pressed for a long time. 
	// When multiple components share the same 1 Used to distinguish components when listening 
}

2. In android application, OnLongClick event indicates that it is triggered by pressing for more than 2 seconds. OnLongClickListener interface and OnClickListener interface
The principle is basically the same, except that this interface is the capture interface of View long press event, that is, the event triggered when a certain View is pressed for a long time, and the corresponding callback method of this interface
As follows:


public void onLongClick(View v){
	// Parameter v Is an event source control that triggers the method when the control is pressed for a long time 
	// When multiple components share the same 1 When a listener , Used to distinguish components 
	// Return value: The return value of this method is 1 A boolean Variable of type 
	// When returning true Hour , Indicates that this event has been handled completely and does not want other callback methods to handle it again 
	// When returning false Hour , Indicates that you have not finished processing the event, and you want other methods to continue processing it. 
}

The following points need to be kept in mind:

A listener is an interface that contains a function that the system calls when an event is triggered In the implementation class, rewrite this function according to your project After the implementation of the listener needs to be bound to the button, just like a headset can make sound, but you can not wear it, you can't hear the sound it makes.

1-like situation is that this View may need this listener, while another View needs another listener, and each monitor
Listeners have their own functions, but when their functions are similar, multiple buttons can be bound to one listener together.

All kinds of controls, there are common events, binding listener function naming convention is setOn ** Listener

Several methods of setting monitoring

1) Enable the Activity to 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) {
			// Click event 
		}
	}

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) {
                    // Click event  
                    //DoSomething
                }
            });
        }
    }

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 establish the click event method in the code 
// Must be public void  Name the same android:onClick ( View v ) 

  public void buttonClick(View v){
	  // Click event 
  }

4) Implement classes


 public class MyClick implements 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);
        MyClick listener = new MyClick();
        back.setOnClickListener(listener);
    }

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);
    }

The above is a detailed explanation of Activity in several listeners and implementation of the details, more about Activity listener information please pay attention to other related articles on this site!


Related articles: