Android input box sorting

Source: Internet
Author: User

/Frameworks/base/services/Java/inputmethodmanagerservice. Java

This is the total control center of all input methods in the system. It manages the following three modules to implement the system's input method framework.

1./frameworks/base/services/Java/windowmanagerservice

Displays input methods and receives user events.

2./frameworks/base/CORE/Java/Android. inputmethodservice/inputmethodservice

The internal logic of the input method, keyboard layout, and word selection finally submit the selected characters through committext. To do something like sogou's input method, I will mainly write articles here.

3. inputmanager

It is called by the UI control (view, textview, edittext, etc.) to operate the input method. For example, enable, disable, and switch the input method.

 

 

The following describes how the inputmethodmanagerservice Control Center interacts with the three modules.

 

1. Interaction with windowmanagerserivce.

First, inputmethodmanagerservice calls iwindowmanager. stub. asinterface (servicemanager. getservice (context. window_service) during initialization to obtain the iwindowmanager proxy and then interacts with windowmanagerservice through iwindowmanager. For example, the following operations:

Call miwindowmanager. addwindowtoken (mcurtoken, windowmanager. layoutparams. type_input_method) to display the input method interface for windowmanagerservice.

Call miwindowmanager. removewindowtoken to disable the input method interface.

Call miwindowmanager. inputmethodclienthasfocus (client) to determine whether the input method is focused.

 

2. Interaction with inputmethodservice.

Inputmethodmanagerservice maintains an arraylist <inputmethodinfo> export hodlist internally. This list is obtained by querying the input method program in the current system through packagemanager when the service is started. Correspondingly, each input method program's androidmanifest. XML has a service, and each service has a tag to tell the system that it is an input method program. The following is a sample from samples/softkeyboard/androidmanifest. xml:

<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"

Package = "com. example. Android. softkeyboard">

<Application Android: Label = "@ string/ime_name">

<Service android: Name = "softkeyboard"

Android: Permission = "android. Permission. bind_input_method">

<Intent-filter>

<Action Android: Name = "android. View. inputmethod"/>

</Intent-filter>

<Meta-data Android: Name = "android. View. Im" Android: Resource = "@ XML/method"/>

</Service>

</Application>

</Manifest>

In addition, inputmethodmanagerservice also has a packagereceiver. When a program installation, deletion, or restart event occurs in the system, mmethodlist is updated. Inputmethodmanagerservice is enabled and disabled. When the input method is switched, it is actually an inputmethodinfo in the mmethodlist operation. Enable or disable the inputmethodservice in inputmethodinfo that represents an input method.

 

3. Interaction with inputmethodmanager

Inputmethodmanager contains an iinputmethodmanager, which is the agent of inputmethodmanagerservice. To enable and disable the input method, some methods in inputmethodmanager call the corresponding methods in iinputmethodmanager. For example:

Mservice. getinputmethodlist () to obtain the input method list.

Mservice. updatestatusicon (imetoken, packagename, iconid) updates the input method icon, that is, the input method icon in the status bar above the screen.

Mservice. finishinput (mclient) hides the current input method. This means that the input method is disabled only when the system is disabled or the input method is switched.

Mservice. showsoftinput (mclient, flags, resultreceiver) opens the current input method.

...

 

 

After introducing the three modules, we will introduce two more things: Implementation of the input method and how to call the input method.

 

1. Take the system's softkeyboard as an example. To implement an input method, at least four things are required: keyboard, keyboardview, candidateview, and softkeyboard.

Candidateview is responsible for displaying the candidate area on the keyboard.

The keyboard parses and saves the keyboard layout, and provides the word selection algorithm for running the program. The keyboard layout is stored in the resource as an XML file. For example, in the Chinese Character Input Method, press B and. The keyboard is responsible for converting the two letters into "dad", "ba" and "ba" and displaying them on candidateview.

Keyboardview is responsible for displaying the buttons we see.

The above two things together form an inputview, which is the soft keyboard we see.

Softkeyboard inherits inputmethodservice. to start an input method, it actually starts an inputmethodservice. When the softkeyboard input method is used, the softkeyboard service is started. Inputmethodservice manages a softinputwindow inherited from dialog, and softinputwindow includes inputview and candidateview.

 

2. How to call the input method?

Speaking of this, we naturally think of edittext. Our team tracked this widget. edittext itself is very simple, and the main code is in textview and view. These two widgets are complicated and cannot be clearly explained together. Here, I will take a small example that our team has done before for reference. It will explain how to call the input method from a view and how to receive the strings passed by the input method.

The origin of the small example is that we want to build a browser. We need to draw what we need on the canvas in surfaceview, enable our main control loop thread, and process events. For example, I want to draw buttons, text, images, and input boxes in the input browser on surfaceview. Of course, these are irrelevant to imageview and textview, and they are all made using my own UI engine. Finally, all the problems are solved, but the input box is stuck. To implement input, you must call edittext. Otherwise, you must connect the input method like edittext. I used to look for relevant information. I have encountered this problem on the Internet, but I have no answer. Finally, it was solved by a cool doll in the team. The code is very simple. There are no more than 20 lines, but there is no information. The source code of the view is too large, and the effort of the view can only be realized by our team... Here, I admire Dr. Zhang's second friend. Without his efforts, there will be no important and important source code for over 20 lines below.

First, define a class inherited from baseinputconnection.

Public class mybaseinputconnection extends baseinputconnection {

Public mybaseinputconnection (view targetview, Boolean fulleditor ){

Super (targetview, fulleditor );

}

Public static string Tx = "";

@ Override

Public Boolean committext (charsequence text, int newcursorposition) {// The input method program outputs the final result by calling this method.

Tx = text. tostring ();

Return true;

}

}

Baseinputconnection is equivalent to a channel between inputmethodservice and view. Whenever inputmethodservice generates a result, it will call the committext method of baseinputconnection to pass the result.

Public class myview extends surfaceview ...{

Inputmethodmanager input = (inputmethodmanager) Context. getsystemservice (context. input_method_service); // obtain inputmethodmanager.

Resultreceiver extends ER = new resultreceiver (new handler () {// defines the event processor.

Public void handlemessage (Message MSG ){

 

}

});

......

Input. showsoftinput (this, 0, MRR); // call this sentence when you want to call the input method.

......

@ Override

Public inputconnection oncreateinputconnection (editorinfo outattrs) {// This method is inherited from view. Pass the custom baseinputconnection channel to inputmethodservice.

Return new mybaseinputconnection (this, false );

}

}

On the low-level interface, you can call the input method and receive the output result of the input method.

Next I would like to mention another thing related to this topic, that is, a bug that was previously fixed:

Http://blog.csdn.net/a345017062/archive/2011/01/04/6116305.aspx

Through this problem, we can see how the input method above webview is implemented. Simply put, webview is a viewgroup with two layers in it. The upper layer is an edittext and the lower layer is a browser page. When the input box of the browser is clicked by the user and the input method needs to be displayed, the edittext position on the upper layer is moved to the input box of the browser. After the size and style of the edittext are high-speed, the edittext and browser pages are integrated, and the effect is good.

Generally, this method is better than calling the input method. You can do less. However, if the product manager is a very imaginative person, you will not be able to meet the input effects he has designed, which may be extremely abnormal but brilliant.

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.