Events are the way to handle user interactions with the application’s interactive components. For example, click the button, enter textview, … In this article I also learn about Events in android
Events Listeners
High level event
- High level events only occur in specific widgets. However, this type of event may not appear on other types of widgets such as buttons
- An interface defined as a listener for a high-level event is often nested within the class for its respective widget.
- Most Events Listeners define a class for the event handler.
Package / Class | Nested Interface | Method |
---|---|---|
android.widget.TextView | OnEditorActionListener | onEditorAction |
android.widget.CompoundButton | OnCheckedChangeListener | onCheckedChanged |
android.widget.RadioButton | OnCheckedChangedListener | onCheckedChanged |
android.widget.Adapter | OnItemSelectedListener | onItemSelected, onNothingSelected |
android.widget.SeekBar | OnSeekBarChangeListener | onProgressChanged, onStartTrackingTouch, onStopTrackingTouch |
Low level event
- Low-level events occur for all widgets. The event listener needs to be nested in the View class.
- Besides clicking, you can also long click (hold for a while).
Package / Class | Nested Interface | Method |
---|---|---|
android.view.View | OnClickListener, OnLongClickListener, OnKeyListener, OnFocusChangeListener, OnTouchListener | onClick, onLongClick, onKey, onFocus, onTouch |
Techniques for handling events
Current class as listener
- The first example shows how to handle the Click event for two buttons using the current class as a listener. This code uses the class (TipCalculatorActivity class) to implement the onClick method defined by the OnClick interface. Then it connects the listener with two nodes. To do that, it uses the named keyword to identify the current class as listener.
For example, we declare the interface as follows:
Then on the activity class, we write a division function and can handle the onClick event
1 2 3 4 | <span class="token keyword">public</span> void <span class="token function">division</span> <span class="token punctuation">(</span> View view <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// todo some thing</span> <span class="token punctuation">}</span> |
Use current class as listener
- Step 1: import interface for event listener
- Step 2: Use the current class implement interface for the listener, then handle the event in the override method
- Step 3: set the listener for the corresponding widget.
For example:
The class is named listener
- Step 1: Create a class implement interface of listener, override methods (event handler).
- Step 2: Declare and initialize the class above.
- Step 3: set the listener for the widget with the parameter that was just created above.
Create anonymous classes as listeners
- Similar to the way number 3, however, not creating a class here will create a variable, then let that variable implement the corresponding listerner interface, set and handle events in that variable. Then set the event for the widget with the variable passed as the parameter implemented above.
Create anonymous inside Class as listener
- No variables are created here. Any variable that wants to set the listener event, the parameter passed we will initialize an interface listener there and perform event handling through the overridden function there.
High level event handling
Checkbox
- Initialize a Checkbox as cb and can handle the event changing the check box selection as follows:
RadioGroup
- Initialize a radioGroup, and the radioButton inside, containing the ID, and can handle the event as follows: