Research on the deletion of soft keyboard input content

Source: Internet
Author: User

 

First, let's describe our problem: when we enter a letter in a dialog box with a soft keyboard, then we want to switch the input method (for example, switching to the input number ), we will find that the letter we entered is missing. When we switch back and enter a number, we will find that the number we just entered is missing, the deleted letter is also displayed.

First, find the processing function of the soft keyboard and analyze it:

Public void onkey (INT primarycode, int [] keycodes ){
If (iswordseparator (primarycode) {// The function defined later
// When the input is interrupted
// Handle Separator
If (mcomposing. Length ()> 0 ){
Committyped (getcurrentinputconnection ());
}
SendKey (primarycode); // After the output is submitted, you must write this special character
Updateshiftkeystate (getcurrentinputeditorinfo (); // you can check whether it is in a special position and the Case sensitivity must be changed.
} Else if (primarycode = keyboard. keycode_delete ){
Handlebackspace ();
} Else if (primarycode = keyboard. keycode_shift) {// but it seems useless on the hard keyboard.
// Case-sensitive Conversion
Handleshift ();
} Else if (primarycode = keyboard. keycode_cancel) {// the key in the lower left corner, disable
Handleclose ();
Return;
} Else if (primarycode = latinkeyboardview. keycode_options) {// This key is like this. The previous latinkeyboardview class defines keycode_options.
// Used to describe the replacement of Long press and close the key in the lower left corner. Tested to be true
// Show a menu or something'
} Else if (primarycode = keyboard. keycode_mode_change // is the key that displays "ABC" or "123"
& Minputview! = NULL ){
Keyboard current = minputview. getkeyboard ();
If (current = msymbolskeyboard | current = msymbolsshiftedkeyboard ){
Current = mqwertykeyboard;
} Else {
Current = msymbolskeyboard;
}
Minputview. setkeyboard (current );
If (current = msymbolskeyboard ){
Current. setshifted (false); // test. If this parameter is set to true, only the Green Point of the shift key is turned on, but it is not changed to another symbol keyboard.
}
} Else {
Handlecharacter (primarycode, keycodes); // This is the real character processing function, not the other control keys.
}
}

From the above analysis, handlecharacter (primarycode, keycodes) is the function that actually processes character processing. Well, we will find the following method:

Private void handlecharacter (INT primarycode, int [] keycodes) {// primaycode is the as code value of the key.
If (isinputviewshown ()){
If (minputview. isshifted ()){
Primarycode = character. touppercase (primarycode); // This truly converts this character into uppercase. After testing, it won't work if it doesn't exist.
// It is not enough to replace the keyboard with uppercase letters. It only solves the problem from the view. You must say this.
}
}
If (isalphabet (primarycode) & mpredictionon) {// The input is a letter and can be displayed in the Candidate Column
Mcomposing. append (char) primarycode); // append (ADD) is to put a character of the current input into mcomposing.
Getcurrentinputconnection (). setcomposingtext (mcomposing, 1); // The latest mcomposing is also displayed in the input target.
Updateshiftkeystate (getcurrentinputeditorinfo (); // each time the input is complete, check whether the value must be capitalized.
Updatecandidates ();
} Else {
Getcurrentinputconnection (). committext (string. valueof (char) primarycode), 1); // For example, when the input is the "'" symbol, this
// The result is to remove all the edited characters. The positive and negative values of the second parameter determine
// Different cursor positions

}
}

From the above analysis, we noticed the IF (isalphabet (primarycode) & mpredictionon) statement. This statement is used to determine whether the letters and the dialog box are displayed. Let's think about our error, isn't the input number different from the input letter? We just want them to calculate it separately? If so, we can print their calculated length mcomposing. length (); after printing, it is found that they are actually calculated and processed separately. At this point, we will think about whether we can combine them for unified processing, so I will modify it as follows:

Private void handlecharacter (INT primarycode, int [] keycodes ){

If (isinputviewshown ()){

If (minputview. isshifted ()){

Primarycode = character. touppercase (primarycode );

}

}

If (isalphabet (primarycode) & mpredictionon ){

Mcomposing. append (char) primarycode );

Getcurrentinputconnection (). setcomposingtext (mcomposing, 1 );

Updateshiftkeystate (getcurrentinputeditorinfo ());

Updatecandidates ();

} Else {

 

// Mod by yanzheng 2011-12-27

Mcomposing. append (char) primarycode );

Getcurrentinputconnection (). setcomposingtext (mcomposing, 1 );

Updateshiftkeystate (getcurrentinputeditorinfo ());

Updatecandidates ();

/*

Getcurrentinputconnection (). committext (

String. valueof (char) primarycode), 1 );

*/

}

}

After compilation and testing, we found that numbers and letters are no longer excluded, but there is another problem, that is, we can't delete the letters and data with a hard keyboard. just delete the soft keyboard, we know that the hard keyboard is processed in onkeydown. We found that there is no deletion key processing in it, so I added the processing in it:

// Add By yanzheng 2011-12-27

Case keyevent. keycode_del:

Log. I (TAG, "keycode_del ");

Handlebackspace ();

Return true; Case keyevent. keycode_del:

Log. I (TAG, "keycode_del ");

Handlebackspace ();

Return true;

// Break;

Case keyevent. keycode_back:

Log. I (TAG, "***** # keycode_back: m_iindexcurkey =" + m_iindexcurkey + "getrepeatcount () =" + event. getrepeatcount ());

If (m_ikeyboardshow = true & minputview! = NULL)

{

Handleclose ();

M_iindexcurkey =-1;

M_ikeyboardshow = false;

Return true;

Call handlebackspace () in the add content; the method is to delete the processing method on the keyboard. After the compilation test is found, I have tested it multiple times and found that sometimes the input content is deleted, and then the deleted content is displayed after the input, in addition, from the printed log, when this phenomenon occurs, the printed data keeps appearing, as if the system had a problem, and this problem also went wrong for a day, later, I heard from my colleagues that this may be caused by frequent APK installation. I only needed to recompile the system, so I tested it several times and found that this was the case.

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.