Differences between Android and j2's (4)

Source: Internet
Author: User
I remember that the great god of von noriman once told us that computers are made up of five parts: memory, controller, memory, input device, and output device. Let's look at the current mobile device. We can see that there are quite a few of the five mentioned above. This is the so-called Sparrow is small and dirty. However, due to device restrictions, mobile phones have huge restrictions on input and output devices. In terms of user input responses, there are only two types of responses, one is the cell phone button, and the other is the touch screen.

In j2s, canvas can respond to buttons and touch screen events. It encapsulates six protected methods and responds to six different events: keypressed (INT keycode) used to respond to button press, keyreleased (INT keycode) is used to respond to button Press Release, keyrepeated (INT keycode) is used to respond to button press for a long time do not release; pointerdragged (int x, int y) used for responding to touch screen dragging, pointerpressed (int x, int y) for responding to touch screen clicking, and pointerreleased (int x, int y) for responding to touch screen releasing. In terms of parameters, keycode tells us which button triggers the event, and X and Y tell us the coordinates (absolute position) when the touch screen is clicked ).

In Android, view can also respond to the preceding two types of events: Boolean onkeydown (INT keycode, keyevent event) for responding to button clicks, Boolean onkeymultiple (INT keycode, int repeatcount, keyevent) is used to respond to repeated clicks of buttons, Boolean onkeyup (INT keycode, keyevent event) is used to respond to button release, and ontouchevent (motionevent event) is used to respond to touch screen events. The official API indicates that the onkeymultiple method always returns false, that is, it does not have a handle, so it must be rewritten for implementation.

in terms of Keyboard Events, the difference between J2EE and Android is that android defines the keyevent class to describe key events. This keyevent is not simple. It can fully describe a key event. Its getaction () method can get the down, up or multiple actions of buttons; its getdowntime () can get the time when the last keydown event occurred; its geteventtime () you can get the time when this event occurred; its getrepeatcount () can get the number of consecutive clicks on the same key (this is largely designed for the onkeymultiple method ). After my practice, I think it is very important to note that when the android underlying layer triggers the keydown event, one thing is very different from the one in the case that we press a key but do not release it, only one keydown event is triggered by keypressed () and then sent to keyrepeated () for processing. However, Android triggers a keydown event every several dozen milliseconds, the onkeydown method continuously responds to events, causing unexpected events. It is not difficult to solve this problem. You can use getaction to determine whether the event is keydown. If so, use geteventtime () to subtract getdowntime (). If the value is too small, you can choose not to respond. This method is simple, but if the user input is too fast, the real input may be ignored, so there is another way: maintain a stack and rewrite onkeydown () and onkeyup () when the method is used, if getaction () is keydown, it is written into the stack. If it is keyup, It is thrown out of the stack. If an event is generated, when it is keydown, if the current top of the stack is keydown, you will choose not to respond to this event. In this way, a long click is equivalent to a single click. In fact, there are some view subclasses. For example, if the button is added to the onlongclick () processing method, even if you select this view and press the selection key for a long time, you can also process it accordingly.

In terms of touch screen, Android only processes ontouchevent (). However, because its parameters contain motionevent, the three events separated under j2s can be distinguished by the getaction () method of motionevent. What is interesting is that the motionevent has a getpressure () method that can obtain the click pressure. It seems that the Android mobile phone has a high degree of precision in components, even the software can obtain the pressure and use it to make some logic.

In addition, the keyevent and motionevent of android can be self-constructed. The keyevent can pass itself to the callback of the keyevent through the dispatch () method, that is, the Event Response Processing Method, in this way, we can build a soft keyboard (it seems easier to do than a Windows soft keyboard at first glance. If we run it as an activity, the focus issue is still very troublesome ), you can also do a lot of other things, but this does not seem to have been done by j2s.

The comparison between the two input event responses is here. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.