Analysis of input method development

Source: Internet
Author: User
Tags lenovo

The Input Method Framework (IMF) has been opened for the Android platform since version 1.5. The IMF is a distinctive design of the Android platform. Its appearance makes it possible for devices without a physical keyboard to be born. At the same time, the IMF opened the development interface for the Android platform input method, providing scalability for the Android platform input method.

The ophone platform inherits the Input Method Framework of the Android platform. This article first introduces the composition, working mechanism and process of the ophone platform IMF, so that developers can better understand how the ophone platform implements text input. Next, we will use a simple example to introduce how to develop a simple input method. Developers should be able to form a preliminary understanding of the input method development on the ophone platform. 1. Introduction to the ophone platform IMFThe IMF of the ophone platform is divided into three main modules by function: client control, input method Service (imms), and Input Method Application (IME ). If you need to thoroughly understand and master the ophone platform's input method development skills, you must understand the working mechanism between modules in the ophone platform IMF. A client control is a system control with the text editing function (such as edittext). It is the starting point for man-machine interaction in the internal input method of the platform. The input method service is one of the underlying basic services of the ophone platform. It manages input methods, including installation, registration, and activation of input methods. An input method application is a preset on the platform or an input method program installed later. The interaction between the three modules is as follows: Next we will introduce the above three modules, and briefly introduce the main objects and services used in each module. 1.1 client controlsThe client controls of the ophone platform are mainly textview and its sub-classes. Client controls are the starting point of human-computer interaction for input methods. Client controls interact with the input method service and the input method application. Take edittext as an example. When the client control receives the focus, the control starts the Input Method Application and displays the keyboard. when the focus is lost, the keyboard is hidden. In addition, when the edittext control receives a long press event, the input method selection menu is displayed. This type of operation is the interaction between the client control and the input method service. In addition, the client control can also pass the text status to the input method application, including the cursor position and Text Selection; accept and display the input text fed back by the input method application. This type of interaction is the interaction between the client control and the input method application. The client controls perform operations on the input method service and the input method application by calling the inputmethodmanager instance. Inputmethodmanager is more like a set of API operations for client controls. It defines a series of operations on the input method application and the input method service. The client control obtains the unique instance through inputmethodmanager. peekinstance. It should be noted that the specific operations of each function call are not implemented in inputmethodmanager. The ophone Input Method Framework defines the iiputmethodmanager interface and the iiputmethodsession interface respectively define the operations for the input method service and the input method application. Inputmethodmanagerservice and inputmethodservice implement the two interfaces respectively. In this way, through the aidl mechanism, the client control can call the implementation in other services across processes. Through this mechanism, the ophone platform reduces the coupling between modules in the input method framework and ensures the scalability of the Input Method Application. The client does not need to know which input method is used, thus achieving simple and direct control. In addition, it should be noted that the client control uses iiputmethodsession to interact with the input method application in one way. That is, it can only transmit information to the input method application and cannot obtain information. The client application obtains information from the input method application through inputconnection. When the input method is started, inputconnection is created by the client control and passed to the input method application, which is called by the Input Method Application for information feedback. 1.2 Input Method ServiceAs a basic service at the underlying platform, the input method service is used to manage Input Method Applications. The main work of the Input Method service is completed by inputmethdomanagerservice (imms. The following describes how to install and switch the input method.
  •  Input Method Installation
Imms contains a receiver, which registers to receive messages about installation and uninstallation of all packages. After receiving such a message, imms queries all programs declared as inputmethod through the system packagemanager and generates a list of available input methods for users to choose from.
  • LInput Method Switching
After you select an input method, imms saves the input method ID as the default input method ID. The ID here is the location ID in the list of available input methods maintained by this input method within imms. When the client starts the input method application through inputmethodmanager, imms extracts the input method from the input method list based on this ID and loads and uses it. 1.3 Input Method ApplicationAn input method application is an application that processes user input behaviors. To be able to run well in the ophone Input Method Framework, all Input Method Applications must inherit specific services. The Input Method Framework of the ophone platform defines a base class of inputmethodservice for the input method application. Inputmethodservice provides a standard implementation of an input method. Defines important functions in the input method life cycle and provides them to developers for corresponding processing. To help developers understand the workflow of the Input Method Application. First, let's take a look at the lifecycle of the Input Method Application:
  • When the user triggers the input method display (the client control obtains the focus), inputmethodservice starts. First, call the oncreate () function. This function is called when the input method is started for the first time. It is suitable for initialization settings, which are the same as those of other services;
  • Call the oncreateinputview () function to create a keyboardview in the function and return the result;
  • Call the oncreatecandidatesview () function to create a candidate area and return it;
  • Call the onstartinputview () function to start the input,
  • After the input is complete, call the onfinishinput () function to end the current input,
  • If you move to the next input box, the onstartinputview and onfinishinput functions are repeatedly called;
  • Call the ondestroy () function when the input method is disabled.
Inputmethodservice implements two important interfaces: inputmethod and inputmethodsession.
  •  Inputmethod
The inputmethod interface defines a set of methods used to manipulate input methods. For example, bindinput, hideinput, and startinput. For system security, this type of interface can only be accessed by the system, and the client control cannot directly call this interface. All input method applications require the bind_input_method permission of the client control as the system's security mechanism. Otherwise, they cannot interact with the input method service.
  •  Inputmethodsession
Inputmethodsession is a secondary interface class of inputmethod. It opens function interfaces that can be called directly for client controls. This includes distributing Keyboard Events to the input method application, updating the cursor position, and updating the problem information selected in the editing area. 2. ophone platform Input Method Development MethodBy introducing the Input Method Framework of the ophone platform, I believe you have a preliminary understanding of the interaction and working mechanism between input methods modules of the ophone platform. The development of input methods in the ophone platform mainly includes the development of the upper Interface UI and the development of the underlying Input Method engine. The development techniques described in this article include how to build an Input Method Application on the ophone platform, how to design a keyboard, and how to develop interfaces. It is worth noting that many core functions of the input method are embodied in the underlying Input Method engine. The function of the input engine includes obtaining candidate words and Lenovo words based on the input characters, and adjusting the Word Frequency of the Management dictionary. In short, the input method engine is a database engine dedicated to language neighborhoods. Based on user key input, candidates are queried in the language database for user selection. These are independent from the development of the ophone platform. The following uses a simple example to describe how to develop an ophone platform Input Method Application. 2. 1. Configure the serviceThe input method is a service in the ophone system. Like other services, the input method must be defined in androidmanifest. xml. Example: View plaincopy to clipboardprint?
  1. <Application Android: Label = "@ string/english_ime_name">
  2. <Service android: Name = "latinime"
  3. Android: Label = "@ string/english_ime_name"
  4. Android: Permission = "android. Permission. bind_input_method">
  5. <Intent-filter>
  6. <Action Android: Name = "android. View. inputmethod"/>
  7. </Intent-filter>
  8. <Meta-data
  9. Android: Name = "android. View. Im"
  10. Android: Resource = "@ XML/method"/>
  11. </Service>
  12. </Application>
This service has the bind_input_method permission, indicating that this is an input method service. Use android in intent-filter. view. the inputmethod action is defined, and the name is Android. view. im meta-data describes some attributes of the input method. Meta-data references an XML file, which is the configuration file of the input method, used to configure some information, for example, whether it is the default input method and whether there are some options for configuring the input method by configuring activity, if activity configuration is specified, you can start the activity in the input method settings on the system settings interface to set the Input Method Configuration items. 2.2 inherit from inputmethodserviceBy extending Android. inputmethodservice. inputmethodservice, you can easily implement an input method service. inputmethodservice provides some system callback functions that can be implemented as needed. The above has been analyzed in detail, and I will not repeat it too much. Constitute an input method application. The most important interface elements include the soft keyboard area and candidate word area. Inputmethodservice sets special callback functions for these two regions, so that developers can flexibly customize and load resource files. The following describes the callback functions in these two regions. 2.2.1 oncreateinputviewThis function is called only once when it is displayed for the first time in the input area (such as the virtual keyboard. The displayed view is returned through return. The default return value is null. If the return value is null, nothing is displayed. When the input area is displayed, you can control it by implementing onevaluateinputviewshown. If you want to replace the displayed input area, you can use setinputview (view. Generate the view of the input area and load the resource file in layout through layoutinflater. In the resource file, it is either a keyboardview or a container composed of several keyboardview. In this function, you usually need to register the keyboard event listener setonkeyboardactionlistener. This listener is used to handle the following Keyboard Events: onkey (INT primarycode, int [] keycodes) onpress (INT primarycode) onrelease (INT primarycode) ontext (charsequence text) 2.2.2 oncreatecandidatesviewThis function is called only once when the Lenovo entry is displayed for the first time. The displayed view is returned through return. If no association is found, null is returned. You can use setcandidatesviewshown (Boolean) to control whether Lenovo entries are displayed. If you want to replace the Lenovo entry, you can use setcandidatesview .. Create a Lenovo entry, use layoutinflater to load the resource file and generate a view. 2.3 soft keyboard AreaOn the ophone platform, the soft keyboard is easy to implement. You can use the keyboard class to create a soft keyboard, which reads the soft keyboard information from the XML file. The soft keyboard information includes the number of lines, the number of keys in each line, and the content represented by each key. 2.3.1 keyboardThe keyboard builds a virtual keyboard by loading XML files. An example of XML file content is as follows: View plaincopy to clipboardprint?
  1. <Keyboard
  2. Android: keywidth = "10% P"
  3. Android: horizontalgap = "0px"
  4. Android: verticalgap = "0px"
  5. Android: keyheight = "@ dimen/key_height" <! -A pre-defined constant in value -->
  6. >
  7. <Row Android: verticalgap = "@ dimen/key_padding">
  8. <Key Android: Codes = "113" Android: keylabel = "Q" Android: keyedgeflags = "Left" Android: popupkeyboard = "@ XML/kbd_popup_template"
  9. Android: popupcharacters = "@ string/alternates_for_q"/>
  10. ......
  11. </Row>
Borqs: keywidth, borqs: horizontalgap, borqs: verticalgap, and borqs: keyheight are used to indicate horizontal Vertical spacing and key length and width respectively. Row indicates a row. Each row contains several keys. The keys on the left and right must be marked with Android: keyedgeflags. Android: popupkeyboard and Android: popupcharacters are used to indicate the resource ID and displayed Text of the keypad popped up after the buttons. After entering the XML file for keyboard building, you need to write a class for specific operations. This class inherits from the keyboard class. In this class, the methods to be implemented are: l constructor: load the keyboard resource file l createkeyfromxml through the constructor: If you need to add certain attributes in the key class, you can inherit the keyboard. in this case, the initialization of these special attributes should be added to this function. 2.3.2 keyboardviewKeyboardview is used to render a virtual keyboard and monitor click and touch events on the keyboard. The input method application needs to construct a keyboard View class inherited from keyboardview. Process all events from the keyboard in this class. Important functions include: l setkeyboard: used to bind keyboard and keyboardview. L onlongpress: used to handle long-press events l getonkeyboardactionlistener: There is a listener in keyboardview-onkeyboardactionlistener. You can use this function to obtain the listener for this keyboard view. The listener uses the onkey function distribution to process keyboard click events. L getkeyboard: use this function to obtain the keyboard class bound to the view. 2.4 candidate word RegionCandidate regions are particularly important for Chinese input methods. In Pinyin input methods, multiple candidate words are displayed, reasonable Arrangement of candidate words is one of the important criteria for determining whether an input method is useful. However, in special cases, candidate regions are not required, for example, entering a number or a password. Candidate regions are implemented by inheriting the view. 2.4.1 candidateviewThe candidate column is different from the keyboard. The ophone Input Method Framework does not encapsulate specific classes for it. The input method candidate column must be integrated with the view for independent implementation. In this class, you need to process all the operations that are clicked in the Candidate column. This includes clicking candidate words. During initialization, the input method application will pass its own reference or the listener specially used to process the click candidate word operation to candidateview. To select a candidate word, call the internal functions of the Input Method Application Service. An important function in candidateview is ondraw (), which draws candidate columns. In candidateview, you need to set the variable used to save the number of candidate word pages. The number of words displayed for the candidate word is calculated based on the number of pages. The width has been adjusted. Use the drawtext method of canvas to write each candidate entry. You can also define a popupwindow in candidateview to display the pop-up text after the point. This pop-up window can be built in candidateview or passed in by candidatecontainer. 2.4.2 candidatecontainerTo save multiple candidateviews. The flip button in the left and right directions on the candidate entry is also managed through this container. This class is not implemented by the ophone platform framework. It draws on the latinime provided by the Android platform. I think this is a good implementation method. The candidateview function is refined and other logical operations except candidate words are processed. In addition, the use of candidatecontainer has another advantage, that is, by saving two candidateviews, the page can be displayed immediately during page turning without reloading, which improves the display efficiency. Candidatecontainer manages multiple views through viewflipper. 3. Summary:From the above introduction to the input method framework and the development method, we can see that the ophone platform provides a good input method framework to easily develop third-party input methods, developers only need to focus on the business logic Implementation of the input method, and the interface part can be handed over to ophone, which can greatly save developers' energy and development costs. The Design of popular big screen touch screen smart terminals, Input Method Applications and human-computer interaction processes has a great impact on the user experience of terminals. It can be said that the input method is one of the most important application modules of Large Screen touch screen smart terminals. While constantly transforming the input method application, the ophone platform also opened up development cooperation on the input method module of the platform, creating opportunities for developers and professional manufacturers to participate in the development of the ophone platform input method. IntroductionXia Bo is a terminal software engineer and a member of the ophone platform development team at the China Mobile Communications Research Institute's Terminal Technology Research Institute. He is mainly responsible for the development, maintenance, and management of IME, layout, widget, and other platform capabilities.

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: 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.