Android keypad button monitoring

Source: Internet
Author: User

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.

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.