Recently, when I was working on a project, I encountered a headache. The problem was that I had to listen to user input from time to time, and most of the user input was completed through a soft keyboard, what should I do if the Android platform does not have a specific monitoring event?
The final solution is to use the edittext and textwatcher classes to assist in listening. The procedure is as follows:
Private class textmonitor implements textwatcher {@ overridepublic void beforetextchanged (charsequence S, int start, int count, int after) {// string str_forward = S. tostring (). length () ;}@ overridepublic void ontextchanged (charsequence S, int start, int before, int count) {// string STR = S. tostring (). substring (start); TV. settext ("Your edited content:" + S. tostring (); If (Str. contains ("\ n") {// enter key }}@ overridepublic void aftertextchanged (editable s) {// input content string str_last = S. tostring (). length ();}}
Controls:
Private textview TV; private edittext edit; TV = (textview) findviewbyid (R. id. showinput); Edit = (edittext) findviewbyid (R. id. inputcontent); Edit. addtextchangedlistener (New textmonitor ());
Layout file:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical"> <textview Android: id = "@ + ID/showinput" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Show your edited content"/> <edittext Android: id = "@ + ID/inputcontent" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: hint = "Input Edit content"/> </linearlayout>
At present, there seems to be no other good method, so we can only indirectly monitor it. You are welcome to propose a better solution.