Android development interception EditText input content, custom input content

Source: Internet
Author: User

1.EditText as a more mature view, in the application development of Android has been very widely used. In some special cases, we may need to customize the input content of the EditText,

Allows only the input of the specified function, such as the degree of the minute and minute angle of the input "ddd:mm:ssssss" format.

Some of the wrong ideas are:

(1) Onkeylistener, the implementation of EditText Onkeylistener can not intercept the input of EditText, Onkeylistener can only listen to some physical key events, such as menu keys, return keys, volume keys and so on,

and the input method as a separate process, edittext and input method of communication is obviously onkeylistener powerless;

(2) Textwatcher, believe that many comrades do this function of the first reaction is to use textwatcher, because Textwatcher can hear the edittext input before and after the changes in the content, but it is only to hear it

, it has no way to actually intercept the input, but only after a certain character input occurs, do an afterthought to edittext. The typical example is the following, assuming I hear the input of "s", but in fact I do not allow "s" input, that

I textwatcher hear "s" entered later, reset the contents of EditText. But the consequence of this is to reset the contents of the EditText, and then callback the Textwatcher of the several methods, resulting in a dead loop. Of course you can

Set a flag bit to stop the loop, but it's more trouble than that. Because you are the first to let the character input, and then manually delete it, then this involves the cursor position adjustment, will be endless ...

2. After describing the above two typical errors (in fact, the LZ has committed), introduce the LZ for the real eidttext input interception implementation.

Since the system to the interface can not be implemented, it is bound to pass the source code to solve the problem. EditText is inherited and TextView, TextView the source code is very large, a function to see

Certainly unrealistic, then search by the keyword, "input" can find "oncreateinputconnection" This method, the literal meaning of creating communication with the input method, the object returned is Inputconnection,

Most likely it is, Inputconnectionwrapper implements the Inputconnection, and the implementation of this class has two very reliable methods, Committext, sendkeyevent, literal meaning to submit text

and key events. See here, presumably the idea is similar:

(1) Overloaded Oncreateinputconnection method, which needs to return a Inputconnection object;

(2) inherit from Inputconnectionwrapper, implement own inputconnection and return in Oncreateinputconnection.

(3) in the custom Inputconnectionwrapper class, implement input and key time interception.

(4) Interception condition: In the Committext method, if the committext of the parent class (that is, Super.committext (text, newcursorposition)) is not intercepted, then the return false indicates interception,

The input method string cannot be transferred to the EditText. In Sendkeyevent, if the sendkeyevent of the parent class is executed (that is, Super.sendkeyevent (event)) then it does not intercept, and if return false indicates interception.

3. At this point, EditText's custom input is the most important link, intercept edittext and input method communication has been successfully implemented, the next is based on your custom rules to reload your Committext and Sendkeyevent method.

A few important methods,

Set cursor Position: SetSelection (cursor-1, cursor-1);

Delete cursor coordinates 0 (m) to the right of the cursor 1 (n) characters: Deletesurroundingtext (0, 1);

In addition, the KeyEvent method of different input methods may be somewhat different, you need to look at the SoftKeyboard sample source code to debug your application, so that it can be compatible with the mainstream input method.

Android development interception EditText input content, custom input content

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.