Android event processing mechanism based on monitoring

Android event processing mechanism based on monitoring

Function: When the user performs various operations on the application interface, the application needs to provide a response to the user's actions, and the process of this response is event processing.

The three elements of monitoring

  • Event
  • Event Source
  • Event Listener

The essence of the event listener: implements a java object sent by a specific interface

Four ways to implement event listeners

1. Activity itself as an event listener: the listener interface is implemented through Activity and the event processing method is implemented.
2. Anonymous inner class form: use anonymous inner class to create a listener object.
3. Inner class or outer class form: define the event listener class as the inner class or ordinary class of the current class.
4. Binding label: bind the event processing method for the specified label in the layout file

Activity itself as the event listener code

  • Create event listener event_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
    <EditText
        android:id="@+id/showTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false"/>
    <!-- android:editable="false":不可编辑的-->
    <Button
        android:id="@+id/clickBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单击我"/>
</LinearLayout>
  • Implement event monitoring interface EventBtnActivity.java
public class EventBtnActivity extends AppCompatActivity
        implements View.OnClickListener {
    //单击按钮
    private Button clickBtn;
    //文字显示
    private TextView showTxt;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_btn);
        //初始化组件
        clickBtn = (Button)findViewById(R.id.clickBtn);
        showTxt = (TextView)findViewById(R.id.showTxt);
        //直接使用Activity作为事件监听器
        clickBtn.setOnClickListener(this);
    }
    //在事件处理方法中编写事件处理代码
    @Override
    public void onClick(View v) {
        //实现事件处理方法
        showTxt.setText("BTN被Activtity单击了!!!");
    }
}

  • The effect is shown in the figure:

Insert picture description here

Disadvantages: Activity's responsibility is mainly to complete the initialization of the interface. Implementing the listener in it is easy to cause confusion in the structure of the program. Most event listeners are only temporarily used once, so anonymous inner classes are more suitable

Anonymous inner class form

  • The event listener here is still the event_btn.xml above, so I won’t repeat it again.
  • Anonymous inner class AnonymousBtnActivity.java
 `public class AnoymousBtnActivity extends AppCompatActivity {
    //单击Button
    private Button clickBtn;
    //文字显示
    private TextView showTxt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_btn);
        //初始化组件
        showTxt = (TextView)findViewById(R.id.showTxt);
        clickBtn = (Button)findViewById(R.id.clickBtn);
        //使用匿名内部类
        clickBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //实现事件处理方法
                showTxt.setText("btn按钮匿名内部类单击了!!!");
            }
        });
    }
}
`
  • effect:
    Insert picture description here

Inner class, outer class form

  • The event listener here is still the event_btn.xml above, so I won’t repeat it again.
  • Inner Class InnerClassBtnActivity.java
public class InnerClassBtnActivity extends AppCompatActivity {
    //单击按钮
    private Button clickBtn;
    //显示文字
    private TextView showTxt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_btn);
        //初始化组件
        clickBtn = (Button)findViewById(R.id.clickBtn);
        showTxt = (TextView)findViewById(R.id.showTxt);
        //直接使用Activity作为事件监听器
        clickBtn.setOnClickListener(new ClickListener());
    }
    //内部类方式定义一个事件监听器
    class ClickListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            //实现事件处理方法
            showTxt.setText("btn按钮内部类");
        }
    }
}
  • Advantages:
    1. The internal listener class can be reused in the current class.
    2. Since the listener is an internal class of the current class, it can access all the interface components of the current class

  • effect:
    Insert picture description here

Bind label:

The Android binding label is directly placed in the interface layout file for the processing of the specified binding time. For most of the components of the Android interface, the attributes of the onClick event are basically supported, and the corresponding attribute is one in. An event processing method defined in the java file

  • event_tag.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
    <EditText
        android:id="@+id/showTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false" />
    <Button
        android:id="@+id/clickBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ***android:onClick="clickMe"***
        android:text="单击我"/>
</LinearLayout>
  • BindTagActivity.java: event handling method
public class event_tagActivity extends AppCompatActivity {
    //单击Button
    private Button clickBtn;
    //显示文本
    private TextView showTxt;
    //

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_tag);
        //初始化组件
        clickBtn = (Button)findViewById(R.id.clickBtn);
        showTxt = (TextView)findViewById(R.id.showTxt);
    }
    public void clickMe(View v){
        //实现事件处理方法
        showTxt.setText("btn按钮标签单击了!!!");
    }
}
  • effect:
    Insert picture description here
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45193294/article/details/115013997