- Android notes Android -based monitoring mechanism
-
1. Event handling of AndroidAndroid event processing includes two parts: the Android event processing mechanism (basic) and Android message transmission mechanism (advanced). The former contains three ways of processing, namely, the incident processing of the monitoring, the processing of incident -based events, and directly bound to the label; the latter contains two processing methods, namely handler message transmission and asynchronous task processing.
1.Android event handling mechanism(1) Monitoring method based on monitoring is usually the usual method is to bind a specific event monitor for the Android interface component, and write the event processing code in the method of the event monitor. (2) The method of processing method based on the callback is usually rewritten to the specific callback method of the Android component. Android provides the corresponding adjustment method of the event for most of the interface components. We only need to rewrite them. When we trigger the corresponding component, the system automatically calls the component according to the specific scenario. (3) Directly bind to the label, that is, we set the event attributes for the specified component label in the interface layout file. The attribute value is a method name of a method, and then the method is determined in Activity to write a specific event processing code. A corresponding response is made when triggering a component that binds android: OnClick tags.2.android message transfer mechanism(1) Handler message transmission (2) asynchronous task processing.2. Monitoring -based event processing 1. Event processing modelThe monitoring event processing model mainly involves three types of objects: (1) EventSource (event source) incident components, that is, the place where the incident occurs, such as buttons, menu, etc. For detailed descriptions of the operation, the incident encapsulates the relevant information of the operation. If you want to obtain the relevant information of the incident in the event source, you can get it through the Event object, such as which key is pressed by the key event, the location of the touch event; (3) EventListener (event monitor) is responsible for monitoring the user’s operation on the source of the event (such as clicking), and corresponding to the user’s various operations. Event processor is actually an event processing method.Note: In fact, the incidental treatment based on monitoring is a commissioned event handling. Ordinary components (event sources) entrust the entire event to a specific object (event monitor). When the source of the event is specified, the system automatically generates the event object and notify the entrusted event monitor, which is by the incident listener. The corresponding event processor to handle this event.2. Steps to handle development based on monitoring events(1) Get the general interface control (event source), that is, the object of the monitoring; (2) Implement the event monitor class. The listener class is a special Java class that must implement a XXXListener interface. It should be noted that there are four main forms of incident listeners:> internal forms: define the event monitor class as the internal class of the current class; Class itself as an event monitor class: allow the Activity itself to implement the listener interface and implement the event processing method;> anonymous internal class: use anonymous internal class to create an event monitor object; The object uses an event monitoring object to register to ordinary components (event sources).3. Simple text editor instanceThrough this example, we will learn how to use the internal forms and other methods to achieve event monitoring.
1.source code(1) Eventtest1.java: Main Activity code123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111package
com.example.androidevent;
import
android.app.Activity;
import
android.graphics.Color;
import
android.graphics.Typeface;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
public
class
EventTest1
extends
Activity
implements
OnClickListener
{
private
TextView content;
int
flag=
0
;
// Font style logo
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
content = (TextView)findViewById(R.id.content);
// 1. Color: internal category form
Button redBtn = (Button)findViewById(R.id.red);
Button greenBtn = (Button)findViewById(R.id.green);
Button blueBtn = (Button)findViewById(R.id.blue);
EventListener inner =
new
EventListener();
// Exemplate an event monitor object
redBtn.setOnClickListener(inner);
// Register the same event monitor to the following interface component
greenBtn.setOnClickListener(inner);
blueBtn.setOnClickListener(inner);
// 2. Font size: external category form
Button bigger = (Button)findViewById(R.id.big);
Button smaller = (Button)findViewById(R.id.small);
OuterListener outer =
new
OuterListener(content);
// instantiated an event monitor object
bigger.setOnClickListener(outer);
smaller.setOnClickListener(outer);
// 3. Font style: class itself as an event monitor
Button boldBtn = (Button)findViewById(R.id.bold);
Button inclineBtn = (Button)findViewById(R.id.incline);
Button normalBtn = (Button)findViewById(R.id.normal);
boldBtn.setOnClickListener(
this
);
// Register the same event monitor to the following interface component
inclineBtn.setOnClickListener(
this
);
normalBtn.setOnClickListener(
this
);
// 4. Anonymous internal class
final
EditText text = (EditText)findViewById(R.id.text);
text.setOnClickListener(
new
OnClickListener(){
public
void
onClick(View v)
{
String contentText=text.getText().toString();
content.setText(contentText);
// Set the content of the editing box to the text display box
}
});
}
/*Method 1: Internal forms
* In the onClight function, determine the ID of the event component through the Getid () method of the view and make a corresponding response */
public
class
EventListener
implements
OnClickListener
{
public
void
onClick(View arg0) {
switch
(arg0.getId())
{
case
R.id.red:
// Set the red button to be pressed
content.setTextColor(Color.RED);
break
;
case
R.id.green:
content.setTextColor(Color.GREEN);
break
;
case
R.id.blue:
content.setTextColor(Color.BLUE);
break
;
default
:
break
;
}
}
}
/*Method 3. Class itself as an event monitor* /
@Override
public
void
onClick(View v)
{
// flag is the style identification. When flag = 0 means default, flag = 1 means tilt, flag = 2 means bold, flag = 3 means tilt and thickened and thickened
switch
(v.getId())
{
case
R.id.bold:
// Set up bold
if
(flag==
1
||flag==
3
)
{
content.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);
// Set the default font style of the system
flag=
3
;
}
else
{
content.setTypeface(Typeface.MONOSPACE,Typeface.BOLD);
flag=
2
;
}
break
;
case
R.id.incline:
if
(flag==
2
|| flag==
3
)
{
content.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);
// Set the default font style of the system
flag=
3
;
}
else
{
content.setTypeface(Typeface.MONOSPACE,Typeface.ITALIC);
flag=
1
;
}
break
;
case
R.id.normal:
content.setTypeface(Typeface.MONOSPACE,Typeface.NORMAL);
// Set the default font style of the system
flag=
0
;
break
;
default
:
break
;
}
}
}
(2) OUTERLISTENER.JAVA: External form event monitor
12345678910111213141516171819202122232425262728293031323334353637383940414243444546package
com.example.androidevent;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.TextView;
/*External class form* /
public
class
OuterListener
implements
OnClickListener
{
private
TextView text;
private
float
size;
// Construction method 1: Pass the text component object as a parameter and assign a value to the class member object
public
OuterListener(TextView t)
{
this
.text = t;
}
// Construction Method 2
public
OuterListener()
{
super
();
}
public
void
onClick(View v)
{
size= text.getTextSize();
// Get text font size
switch
(v.getId())
{
case
R.id.big :
{
size+=
4
;
break
;
}
case
R.id.small :
{
size-=
4
;
break
;
}
default
:
break
;
}
// Set the highest value limit
if
(size<=
8
)
size=
8
;
if
(size>=
72
)
size=
72
;
text.setTextSize(size);
}
}
(2) Effect demonstration
(3) Source code analysisA. Internal class form: The incident monitor class is defined as the internal class of the current class. > Advantages: First, the internal class can be used in the current class to reuse the listener class, that is, multiple event sources can register the same monitor; the second is to use the internal class to freely access all interface controls of the external class. The internal class is essentially essentially It is a member of the outer class. > Scope of application: The internal form is more suitable for the situation where multiple events sources are registered at the same time. b. External class form: define the incident monitor class as an external class;> Disadvantages: First, because the incident listener usually belongs to a specific GUI, it is defined as an external class that is not conducive to improving the internal agglomeration of the program. The structural method of the external class can convey a text object and assign it to the members of the current class object to operate it.programmingNot concise enough. > Scope of use: If an event listener really needs to be shared by multiple GUI interfaces, and it is mainly to complete the implementation of a certain business logic, you can consider using an external class to define the event monitor class. c. The class itself as an event monitor class: Let the Activity itself realize the listener interface and implement the event processing method; You do not need to be entrusted to others, you can define the event processor method directly in the Activity class. This form is very concise. > Disadvantages: It may cause confusion in program structure. The main responsibility of Activity should be to complete the interface initialization work, but at this time, it also needs to include an event processor method to cause confusion; if the Activity interface class needs to implement the listener interface, it feels weird to give people weird Essence d. Anonymous internal class: Use anonymous internal class to create event monitor objects> Advantages: Event processors have no reusing value (replication code can usually be abstracted into business logic methods), so most event listeners are just only incident listeners. It is used temporarily, so it is more suitable to use anonymous internal type of event monitor. In fact, this form is also the most widely used event monitor form. Note: When a local member variable is accessed in an anonymous internal class, the member variable must be decorated with, and there is no requirement for member variables.4. Common Event Listener Interface and MethodsThe four forms aboveand above need to be treated by the ONCLICK member processing method of the incident, according to the interfacecomponentID to provide event response. In addition, except for anonymous internal forms, the remaining three types need to inherit the corresponding event monitor interface.1. Common event listener interface and processing method
event
interface
treatment method
Description
Click event
View.OnClickListener
abstract void onClick (View v)
Click to trigger when clicking the component
Click the event
View.OnLongClickListener
abstract boolean onLongClick (View v)
When long pressing the component, triggers
Keyboard event
View.OnKeyListener
abstract boolean onKey(View v, int keyCode, KeyEvent event)
Treatment of keyboard events
Focus event
View.OnFocusChangeListener
abstract void onFocusChange (View v, boolean hasFocus)
When the focus changes, triggers
touch event
View.OnTouchListener
abstract boolean onTouch (View v, MotionEvent event)
Conditioning incident
2.View Class Common Event Registration Method
method
description
void setOnClickListener(View.OnClickListener l)
RegisterClickevent
void setOnLongClickListener(View.OnLongClickListener l)
RegisterLong pressevent
void setOnKeyListener(View.OnKeyListener l)
Registrationevent
void setOnFocusChangeListener(View.OnFocusChangeListener l)
RegistrationFocus changeevent
void setOnTouchListener(View.OnTouchListener l)
Registertouchevent
void setOnCreateContextMenuListener(View.OnCreateContextMenuListener l)