Android: Hide IME Soft keyboard

Source: Internet
Author: User

1. Overview

1) to LinearLayout register Click event, click to hide the input method soft keyboard. Why call a soft keyboard, it's strange. The object of the input method is obtained through the activity's Getsystem (Context.input_method_service), and then it is hidden away.

2) EditText the input character reaches the maximum value when the Input method soft keyboard is hidden. Register EditText Textwatcher Event, by judging the number of characters entered to reach the maximum length when the input method soft keyboard is hidden.

2.Activity Backend Code

public class Edithideactivity extends Appcompatactivity implements View.onclicklistener {private LinearLayout ll_hide;    Private EditText Et_phone;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_edit_hide);        Ll_hide = (linearlayout) Findviewbyid (r.id.ll_hide);        Et_phone = (EditText) Findviewbyid (R.id.et_phone);        To LinearLayout Register Click Listen, click to hide the Input method Ll_hide.setonclicklistener (this);    To EditText register TextChanged Monitor, text changes will trigger the listener event Et_phone.addtextchangedlistener (new Hidetextwatcher (Et_phone)); }//implements the Onclicklistener onclick event @Override public void onclick (view view) {if (View.getid () = = R.id.ll_h        IDE) {Hideoneinputmethod (edithideactivity.this, Et_phone); }} private void Hideoneinputmethod (Activity Act, View v) {//actually not just et_other the soft keyboard will close, other edit box's soft keyboard will also close//because method to get the windowtoken of the view, the token on each pageis the only inputmethodmanager IMM = (inputmethodmanager) act.getsystemservice (Context.input_method_service);    Imm.hidesoftinputfromwindow (V.getwindowtoken (), 0); } private void Hideallinputmethod (Activity Act) {Inputmethodmanager IMM = (inputmethodmanager) act.getsystemser        Vice (Context.input_method_service);        if (imm.isactive () = = True) {imm.togglesoftinput (0, inputmethodmanager.hide_not_always);        }}//Get EditText maximum length private int getmaxlength (EditText et) {int length = 0;            try {inputfilter[] inputfilters = Et.getfilters ();                Traverse all filters for (Inputfilter filter:inputfilters) {class<?> c = filter.getclass (); Whether there is a inputfilter$lengthfilter filter if (C.getname (). Equals ("Android.text.inputfilter$lengthfilter")                    {//field namespace is: Java.lang.reflect.Field field[] f = c.getdeclaredfields (); //Traverse filter for all fields for (field field:f) {if (Field.getname (). Equals ("Mmax")) {                            Field.setaccessible (TRUE);                        Length = (Integer) field.get (filter);        }}}}} catch (Exception ex) {ex.printstacktrace ();    } return length;        } Private class Hidetextwatcher implements Textwatcher {private Charsequence mstr;        Private EditText MView;        private int mmaxlength;            Through the constructor to inject the EditText to be listened to, hold EditText facilitates the operation public Hidetextwatcher (EditText v) {super () in the event;            MView = v;        Gets the maximum length of the edittext mmaxlength = Getmaxlength (v);        }//charsequence charsequence: Content before text changes//int I: Start position when text starts to change, calculate//int i1 from 0: The number of words to be changed, that is, the number of selected text that will be replaced int I2: The number of text words added after the change, that is, the number of text after the selected text @Override public void beforetextchanged (ChaRsequence charsequence, int i, int i1, int i2) {}//charsequence charsequence: Content after text changed//int I: Text start Change the starting position, starting from 0 to calculate the//int i1: The text word number to be changed, that is, the selected text has been replaced//int I2: The number of words added after the change, that is, replace the text after the selected text word count @Override p        ublic void OnTextChanged (charsequence charsequence, int i, int i1, int i2) {mstr = charsequence;            }//editable Editable: The final text after the change @Override public void aftertextchanged (Editable Editable) {            if (MSTR = = NULL | | mstr.length () = = 0) {return; If the Else if (mstr.length () = = Mmaxlength) {//input characters reach maximum, hide the IME hideallinputmethod (edithideacti            Vity.this); }        }    }}

  

3.layout Layout File Code

<?xml version= "1.0" encoding= "Utf-8"? ><android.support.constraint.constraintlayout xmlns:android= "http:/ /schemas.android.com/apk/res/android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools="/HTTP/ Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context = "Com.example.administrator.middledemo.EditHideActivity" > <linearlayout android:id= "@+id/ll_hide" a Ndroid:layout_width= "368DP" android:layout_height= "495DP" android:orientation= "vertical" tools:layout _editor_absolutey= "8DP" tools:layout_editor_absolutex= "8DP" > <view android:layout_width= "ma            Tch_parent "android:layout_height=" 20DP "/> <edittext android:id=" @+id/et_phone "            Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:ems= "10"           Android:maxlength= "11" Android:inputtype= "number" style= "@style/text_normal"/> </linearlayout></android.support.constr Aint. Constraintlayout>

  

Android: Hide IME Soft keyboard

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.