Use OnLayout to determine whether the keyboard is closed or popped in a custom view

Source: Internet
Author: User

Controlling the soft keyboard in Android development is a very frustrating thing because Android does not open an interface for developers to easily control the pop-up and hide of the keyboard (this is the time to envy the iOS developers ...). )。 So if I have a requirement: I have an input box in my interface, I need to control it according to the state of the soft keyboard, if the keyboard pops up, the edittext will appear green, and if hidden, it will appear dimmed. How can I control it, today think of a simple method, try it is also possible, but there are limitations, but better than nothing. Send it out and take it with you.

The idea is to rewrite the OnLayout method, get the interface height, this time need to set the activity in the Androidmanfest file android:windowsoftinputmode= "Adjustresize", This will only change at the height of the interface.

Words don't say much, on the code:

Package Com.example.testline;import Android.content.context;import Android.util.attributeset;import    Android.widget.relativelayout;public class Animview extends Relativelayout {private int tempbottom = 0;    private int bottom = 0;    Private Layoutchangelistener listener;    Private Boolean IsFirstRun = true;    Public Animview (Context context) {super (context);    } public Animview (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);    } public Animview (context context, AttributeSet Attrs) {Super (context, attrs);    } public void Setlistener (Layoutchangelistener listener) {This.listener = listener;        } public interface Layoutchangelistener {void Keyboardclose ();    void Keyboardopen ();  } @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {super.onlayout (changed, L,        T, R, b);        if (isFirstRun) {bottom = B; } if((Tempbottom-b) < 0)            {System.out.println ("close");        Listener.keyboardclose ();            } else if ((tempbottom-b) > Bottom/3) {System.out.println ("unfold");        Listener.keyboardopen ();        } IsFirstRun = false;    Tempbottom = b; }}


public class Mainactivity extends Activity implements Layoutchangelistener {    private EditText et;    Private Animview av;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        ET = (EditText) Findviewbyid (r.id.et);        AV = (animview) Findviewbyid (R.ID.AV);        Av.setlistener (this);    }    @Override public    void Keyboardclose () {        et.setbackgroundresource (r.color.yellow);    }    @Override public    void Keyboardopen () {        et.setbackgroundresource (r.color.red);    }}



<com.example.testline.animview xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= " Http://schemas.android.com/tools "    android:id=" @+id/av "    android:layout_width=" Fill_parent "    android: layout_height= "Fill_parent" >    <edittext        android:id= "@+id/et" android:layout_width= "Match_        Parent "        android:layout_height=" wrap_content "        /></com.example.testline.animview>

It's so simple and easy to use, but you need to set android:windowsoftinputmode= "Adjustresize". What you need can be taken away.

Author original, welcome reprint, Reprint please indicate the original address

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use OnLayout to determine whether the keyboard is closed or popped in a custom view

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.