Android Settings system return key and soft keyboard get focus issue

Source: Internet
Author: User

Many friends have had such a problem, when the bottom pop-up comment box, there are a few questions, one is how to let the software disk pop-up in the input box, about this problem, I also encountered, resolved for a long time, some in the layout settings or code setfocusable (TRUE), and no use, Again in code or code to set Requestfocus (), still no use, in fact, this solution is not wrong, but why it is useless, because it is not in time to set up in the software disk popup, so first proposed to hide and display the software disk method

private void Onfocuschange (Boolean Hasfocus) {Final Boolean isfocus = Hasfocus; (new Handler ()). postdelayed (New Runnable ( {public void Run () {Inputmethodmanager IMM = (Inputmethodmanager) commentedit.getcontext (). Getsystemservice (Input_ Method_service), if (isfocus) {//Display Input Method Imm.togglesoftinput (0, inputmethodmanager.hide_not_always); Commentedit.setfocusable (True); Commentedit.requestfocus ();} else {//Hide IME Imm.hidesoftinputfromwindow (Commentedit.getwindowtoken (), 0);}}}, 100);}




Where Commentedit is the variable of your layout's Editext ID, this method writes very clearly, displays the input method and hides the input method, when you call in other methods, just set Onfoucschange (true) or Onfoucschange (false ), then the question came, here in 2 cases,

1. Method I have included the input box to get the focus of the method

Commentedit.setfocusable (True); Commentedit.requestfocus ();
Then how to hide after the input, yes, just said Onfoucschange (false), you can set the click event outside the input box, and then call this method, and then set the previous layout to display can be setvisitible (true), Let's take a look at the example.

Everyone look at the picture can be learned that the focus has itself to get in the input box, not I click on or get, is this effect, the way to cancel the input box, I set up 3 kinds of, one is the software disk, I set the cancellation, one is in the comment list set, there is a place where I marked red, This my mobile phone comes with the return button, the emphasis is on, many people click the system comes back key, found unable to cancel, and then some people found on the Internet to set the system's return key, in the inside to hide the operation, but hidden later, the return key does not have the function of return, and then the person plus finish () , the results found hidden at the same time also returned, this is not what we want to effect, we want to click, input disk to hide, and then the next click to return, below I directly posted code, very simple, we also very good understanding

, first send with handler in all variables

  private static Boolean isexit = false;
Handler Mhandler = new Handler () {        @Override public        void Handlemessage (Message msg) {            super.handlemessage ( msg);            Isexit = false;        }    };
Then rewrite the onkeydown method

@Overridepublic boolean onKeyDown (int keycode, keyevent event) {         if (keycode = = Keyevent.keycode_back                 & & Event.getrepeatcount () = = 0) {    bottomlinear.setvisibility (view.visible); Commentlinear.setvisibility ( View.gone); Onfocuschange (false); Exit ();        }         return true;     }
private void Exit () {        if (!isexit) {            isexit = true;            Toast.maketext (Getapplicationcontext (), "Press again to exit the program",                    Toast.length_short). Show ();            Use handler delay to send Change status information            mhandler.sendemptymessagedelayed (0, +);}        else {            finish ()        }    }


In this method, I set the

bottomlinear.setvisibility (view.visible);
Commentlinear.setvisibility (View.gone);
Is my own layout, is what you see on the map does not pop up the layout of the input box, the people according to the actual situation locked.

Well, these things have, we can normal operation of your soft keyboard, and later encountered these problems can not be afraid of

Give it a good comment.






Android Settings system return key and soft keyboard get focus issue

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.