Android Development Foundation event processing

Android Event Processing

1--based event processing monitor

Based on listening event processing, in fact, bind event listeners for the UI components.
In the event listener processing model involves the following three objects.
(1) Event Source (Event Source): generally refers to the various components.
(2) Event (event): generally refers to a user operation, the event package a variety of specified event occurs on the component.
(3) Event Listener (event listeners): Source is responsible for the event listener event occurred, and respond to the event.

First, we briefly explain the process flow of events, first with an event listener to listen for the event source, event trigger action when outside source, it will generate an event, the event listener gets the event and then process the event by event handlers .
Here Insert Picture Description
Next, we use an example to show you, it is the button to set the event listeners to handle events triggered by the button.
Warming: Create a Project, create a Module in the Project, this is a routine operation, not repeat them.
Project is to create a project step File-> new-> new project, follow the prompts next to it. To create a Module is File-> new-> new Module, and then follow the prompts next to it.
I have created a name for the demo Module, as shown.
Here Insert Picture DescriptionFirst, I open the Design activity_main.xml interface, designed to meet, we add a button layout, we can TextView layout deleted, not here, add a button, as follows:

  <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单击" />

Id is the name of the button the text name of the button is a button, clicking the button, the button width and height of the package is the default content. FIG interface layout after changing for the better.
Here Insert Picture DescriptionHere we Main.Activity.java folder, the bind button Id, set the listener to listen, add Main.Activity.java folder code is as follows: I have explained the meaning of the code in the comments. I use anonymous inner classes as a listener.
Note: If you are prompted an error after you add the code, put the mouse on the wrong place, buttons shortcuts Alt + Enter will automatically import the default package for you.

 Button button = (Button) findViewById(R.id.button) ; //绑定按钮Id,这个按钮名称一定要与布局文件中的一样
        button.setOnClickListener(new View.OnClickListener() { //设置按钮监听器,通过匿名内部类来实现
            @Override
            public void onClick(View view) { //Android studio自动重写Onclick 方法
                Toast.makeText(MainActivity.this,"单击了按钮", Toast.LENGTH_SHORT).show() ;//用Toast去显示按钮被点击了,显示时长为短暂
            }
        });

Add Main.Activity.java folder interface after finished code looks
Here Insert Picture Descriptiongood listener a button to get, let's run it and see results, click on the figure shown, select the demo, please click on the triangle run, will pop up a prompt box, click OK on it.
Here Insert Picture DescriptionAfter about a minute, run out the interface, click on the button, there is an effect on behalf monitor success.
Here Insert Picture Description

2- callback-based event processing

Which is actually processing a callback event on which a callback method to rewrite it.
We often write callback methods are mainly the following:
(1) the OnKeyDown (): Press a button on the trigger assembly
(2) OnTouchEvent (): Touch Screen trigger when the trigger
(3) OnKeyUp (): release a a key trigger
of course more than these, there is a long press a key trigger, trigger trackball triggering.

Let's demonstrate an example.

We rewrite the callback method in Main.Activity.java folder, the code is as follows

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Toast.makeText(MainActivity.this, "按下", Toast.LENGTH_SHORT).show();
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Toast.makeText(MainActivity.this, "抬起", Toast.LENGTH_SHORT).show();
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Toast.makeText(MainActivity.this, "触摸", Toast.LENGTH_SHORT).show();
        return super.onTouchEvent(event);

    }

Respectively is OnKeyDown (): by pressing a touch on the assembly; OnTouchEvent (): Trigger triggered when the touch screen; OnKeyUp (): release the three rewrite a key trigger callback method, Main rewritten interface .Activity.java folder is as follows:
Here Insert Picture DescriptionLet's run it and see the results, click on the triangle run, will pop up a prompt box, click OK on it.
About twelve minutes, we click on the button next to the volume will be prompted to press, after you let go, you will be prompted to lift, you use the mouse to click on a touch screen interface, it will display touch, as shown in Figure representatives get.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
These are the basic content of android event handling, if you have help, a point and then go like, thank you!

Published 45 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/nuist_NJUPT/article/details/105080532