SoftKeyboard for Android SDK sample

Source: Internet
Author: User
Tags call back

Http://www.cnmsdn.com/html/201003/1268842572ID2157.html

There are four files:

LatinKeyboard. java:

This is the soft Keyboard class, which directly inherits the Keyboard class to implement a Keyboard that inputs Latin. It also defines an internal class called LatinKey, which directly inherits the Key to define a separate Key. Its unique overloaded function is isInside (int x, int y ), used to determine whether a coordinate is within the key. It reloads to determine whether the key is a CANCEL key. If yes, it reduces the Y coordinate by 10 PX. According to his explanation, it is used to restore the target area of the key that can turn off the keyboard.

In LatinKeyboard, A createKeyFromXml function is reloaded. This is a callback function called when the keyboard depicts the key. It loads a key from an xml resource file, and placed at the coordinates of (x, y. It also determines whether the key is a return key and saves it.

This also defines a function: setImeOptions, which sets the appropriate tag for the keyboard's return key based on the current information in the edit box. This is easy to understand. As I mentioned earlier, different input boxes will generate different return key labels or icons. In this function, some imeOption bit information is used, such as IME_MASK_ACTION. It is mainly used to view the EditorInfo Action information, which includes:

IME_ACTION_GO: This is the go operation that brings users to the target of the input box. Then the validation key will not have an icon, but there is a label: GO

IME_ACTION_NEXT: this is the next operation, which brings the user into the text box to write an input box. For example, when editing a short message, the content is the next text field in the recipient's mobile phone number box. It is just a NEXT label.

IME_ACTION_SEARCH; this is a search operation, and its default action is search. For example, when you enter an image in the URL box, the search operation is performed by default. It provides an icon like a magnifier.

IME-ACTION_SEND: This is the send operation, and its default action is to send the current content. For example, when a short message is entered in the content box, a sending operation is usually followed. It also provides only one Label: SEND

DEFAULT: by DEFAULT, the text box does not have any special requirements, so you only need to set the return icon.

So far, this class has been completed. In a simple sentence, define the layout of a Keyboard in xml format and inherit a Keyboard class.

 

LatinKeyboardView. java

This class is the simplest class. The previously defined keyboard class is generally just a concept and cannot be used to create a UI. Therefore, a VIEW class is needed for rendering. This class simply inherits the KeyboardView class, and then it is strange to overload a method, that is, the onLongPress function, which is called when there is a long press event. It first checks whether the key is a CANCELJIAN key. If yes, it sends an event to the keyboard where the OPTIONS key is pressed. In my understanding, it seems that the CANCEL key is blocked and an unknown code key is sent.

Candidateview. java

This class is the View class of the candidate key, which can be directly inherited from the View class. There are a lot of private variables in it, which are very disappointing.

MService: This is the candidateView host class, that is, why the view is input method service.

MSuggestions: This is a suggestion. For example, when we enter some letters, the input method is recommended based on input.

MSelectedIndex: Index of the selected words.

MSelectionHighlight: this class is used to depict the highlight of the selected area.

MTypedWordValid; whether the entered word is valid or not.

MBgPadding: fill area of the background.

MWordWidth; this is the width of each word in the Candidate word.

MWordX: The X coordinate of each candidate word. With these two variables, the candidate key can be accurately drawn on the screen.

MColor *: various colors are defined.

MPaint: This is a drawing class, which will be used later

MVerticalPadding; vertical fill area.

MTargetScrollX: the horizontal coordinate of the target rolling, that is, where the target is to be rolled.

MTotalWidth: Total Width

MGestureDetector: This is a gesture monitor.

Then there are various methods. This class is well designed.

CandidateView: constructor. Its parameter is context. The input method can be passed to the constructor. It should be the context of the system. In this class, we mainly initialize some variables. First, mSelectionHighlight is initialized. It is a drawing class. It is initialized with the Default background of android, and some States of the drawing are set. Then, set the background color for the entire iew and initialize each color value. Note that resources are obtained through the system resource manager instead of displayed. Then a gesture detector is initialized. Its Listener reloads a method called onScroll, which is triggered when the gesture detector finds a scroll action. In this function, the main function is to judge the sliding. For example, first obtain the last x coordinate and add the moving distance to check whether the maximum width is exceeded, or whether the smallest left is exceeded. The final distance after the target is moved is sx, and then the view is moved to sx. Then, you need to set whether the horizontal side needs to fade in horizontal scrolling, set whether the candidate view needs to be draw, and finally set whether the horizontal and vertical scroll bars need to be displayed.

 

SetService is used to set the host input method.

ComputeHorizontalScrollRange, which indicates the horizontal scrolling area of the VIEW and returns the overall width of the candidate VIEW.

OnMeasure: the overloaded view class, which is called by the parent view during the layout stage. For example, if the parent view needs to be laid out based on the size of its child views, you need to call back this function to view the size of the view. It calculates its expected width first, and CALLS resolveSize to see if it can get a 50px width. Then, it calculates the desired height. I didn't understand the calculation here: text font size + vertical fill size + (top of the highlighted area + coordinates below ). I didn't understand what the values of the two sides are in the coordinates. What does this mean. Note that you must call setMeasureDimension to save the width and height. Otherwise, an exception may occur.

OnDraw: Reload it to draw images. It provides a canvas. If it is empty, it calls the parent class to draw the canvas directly. First, you need to determine whether a candidate word exists. If no candidate word exists, you do not need to draw it. Then, you need to initialize the filling Area of the background and obtain it directly from the background of the view. Then draw each candidate word. For each candidate word, obtain its text, calculate its fast read, and then add gaps on both sides. Then, you can determine whether the current word is selected: the position of the touch + the scroll position. If it is between the left and right of the current word, the highlighted area is drawn on the canvas, the size of the highlighted area is the size of the current word, and the selected word index is saved. The most important thing is to draw the text on the canvas of the candidate word. It makes a judgment to determine which word is the recommendation word. By default, it is the first word of a candidate word, but it determines whether the first word is valid. If yes, the first word is a candidate word, otherwise, the second word is the rough candidate, and then draw. Finally, we need to draw a line to separate candidate words. The total width mentioned above can be obtained after all words are drawn. At the end of the page, you need to determine whether the target rolling is current. If not, you need to scroll over.

CrollToTarget: Roll to the target region. First obtain the current value, and then add a rolling distance to check whether the value has exceeded. Then, adjust the value and scroll to the corresponding coordinates.

SetSuggestions: Set candidate words. After setting, draw the image.

OnTouchEvent: called when a touch event is generated. Determine the time here: 1. up, but not dynamic; 2. move: to move left, you must manually select candidate words. 3. if it is up, you also need to manually select candidate words.

TakeSuggestionAt; select the word at coordinate x. The processing is that the user clicks the keyboard gently, that is, select the candidate word.

RemoveHighlight: Remove highlight.

All in all, this class is a candidate word class for the entire input method, which is not yet fully understood yet. You need to debug it before you can understand it. Please wait.

SoftKeyboard. java: the most important and complex class.

 

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.