Android determines the soft keyboard status (display, hide)

Source: Internet
Author: User

First, there is a picture of the truth:


Custom RelativeLayout

[Java]
Package com. demo. softkeyboard;
 
Import android. content. Context;
Import android. util. AttributeSet;
Import android. widget. RelativeLayout;
 
Public class KeyboardListenRelativeLayout extends RelativeLayout {

Private static final String TAG = KeyboardListenRelativeLayout. class. getSimpleName ();

Public static final byte KEYBOARD_STATE_SHOW =-3;
Public static final byte KEYBOARD_STATE_HIDE =-2;
Public static final byte KEYBOARD_STATE_INIT =-1;

Private boolean mHasInit = false;
Private boolean mHasKeyboard = false;
Private int mHeight;

Private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;

Public KeyboardListenRelativeLayout (Context context ){
Super (context );
}
Public KeyboardListenRelativeLayout (Context context, AttributeSet attrs ){
Super (context, attrs );
}

Public KeyboardListenRelativeLayout (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}

Public void setOnKeyboardStateChangedListener (IOnKeyboardStateChangedListener onKeyboardStateChangedListener ){
This. onKeyboardStateChangedListener = onKeyboardStateChangedListener;
}

@ Override
Protected void onLayout (boolean changed, int l, int t, int r, int B ){
Super. onLayout (changed, l, t, r, B );
If (! MHasInit ){
MHasInit = true;
MHeight = B;
If (onKeyboardStateChangedListener! = Null ){
OnKeyboardStateChangedListener. onKeyboardStateChanged (KEYBOARD_STATE_INIT );
}
} Else {
MHeight = mHeight <B? B: mHeight;
}

If (mHasInit & mHeight> B ){
MHasKeyboard = true;
If (onKeyboardStateChangedListener! = Null ){
OnKeyboardStateChangedListener. onKeyboardStateChanged (KEYBOARD_STATE_SHOW );
}
}
If (mHasInit & mHasKeyboard & mHeight = B ){
MHasKeyboard = false;
If (onKeyboardStateChangedListener! = Null ){
OnKeyboardStateChangedListener. onKeyboardStateChanged (KEYBOARD_STATE_HIDE );
}
}
}

Public interface IOnKeyboardStateChangedListener {
Public void onKeyboardStateChanged (int state );
}
}
Package com. demo. softkeyboard;

Import android. content. Context;
Import android. util. AttributeSet;
Import android. widget. RelativeLayout;

Public class KeyboardListenRelativeLayout extends RelativeLayout {
 
Private static final String TAG = KeyboardListenRelativeLayout. class. getSimpleName ();
 
Public static final byte KEYBOARD_STATE_SHOW =-3;
Public static final byte KEYBOARD_STATE_HIDE =-2;
Public static final byte KEYBOARD_STATE_INIT =-1;
 
Private boolean mHasInit = false;
Private boolean mHasKeyboard = false;
Private int mHeight;
 
Private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;
 
Public KeyboardListenRelativeLayout (Context context ){
Super (context );
}
Public KeyboardListenRelativeLayout (Context context, AttributeSet attrs ){
Super (context, attrs );
}
 
Public KeyboardListenRelativeLayout (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}
 
Public void setOnKeyboardStateChangedListener (IOnKeyboardStateChangedListener onKeyboardStateChangedListener ){
This. onKeyboardStateChangedListener = onKeyboardStateChangedListener;
}
 
@ Override
Protected void onLayout (boolean changed, int l, int t, int r, int B ){
Super. onLayout (changed, l, t, r, B );
If (! MHasInit ){
MHasInit = true;
MHeight = B;
If (onKeyboardStateChangedListener! = Null ){
OnKeyboardStateChangedListener. onKeyboardStateChanged (KEYBOARD_STATE_INIT );
}
} Else {
MHeight = mHeight <B? B: mHeight;
}

If (mHasInit & mHeight> B ){
MHasKeyboard = true;
If (onKeyboardStateChangedListener! = Null ){
OnKeyboardStateChangedListener. onKeyboardStateChanged (KEYBOARD_STATE_SHOW );
}
}
If (mHasInit & mHasKeyboard & mHeight = B ){
MHasKeyboard = false;
If (onKeyboardStateChangedListener! = Null ){
OnKeyboardStateChangedListener. onKeyboardStateChanged (KEYBOARD_STATE_HIDE );
}
}
}
 
Public interface IOnKeyboardStateChangedListener {
Public void onKeyboardStateChanged (int state );
}
}

Usage:

[Java]
Package com. demo. softkeyboard;
 
Import com. demo. softkeyboard. KeyboardListenRelativeLayout. IOnKeyboardStateChangedListener;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. EditText;
Import android. widget. TextView;
 
/**
* Soft Keyboard listener Demo
* @ Author qiaoning
*
*/
Public class SoftKeyboardListenDemoActivity extends Activity {

Private EditText editText;
KeyboardListenRelativeLayout relativeLayout;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

RelativeLayout = (KeyboardListenRelativeLayout) findViewById (R. id. keyboardRelativeLayout );
EditText = (EditText) findViewById (R. id. editText );

RelativeLayout. setOnKeyboardStateChangedListener (new IOnKeyboardStateChangedListener (){

Public void onKeyboardStateChanged (int state ){
Switch (state ){
Case KeyboardListenRelativeLayout. KEYBOARD_STATE_HIDE: // hide the keyboard
EditText. setVisibility (View. VISIBLE );
Break;
Case KeyboardListenRelativeLayout. KEYBOARD_STATE_SHOW: // displayed on the keyboard
EditText. setVisibility (View. GONE );
Break;
Default:
Break;
}
}
});
}
}
Package com. demo. softkeyboard;

Import com. demo. softkeyboard. KeyboardListenRelativeLayout. IOnKeyboardStateChangedListener;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. EditText;
Import android. widget. TextView;

/**
* Soft Keyboard listener Demo
* @ Author qiaoning
*
*/
Public class SoftKeyboardListenDemoActivity extends Activity {
 
Private EditText editText;
KeyboardListenRelativeLayout relativeLayout;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

RelativeLayout = (KeyboardListenRelativeLayout) findViewById (R. id. keyboardRelativeLayout );
EditText = (EditText) findViewById (R. id. editText );

RelativeLayout. setOnKeyboardStateChangedListener (new IOnKeyboardStateChangedListener (){

Public void onKeyboardStateChanged (int state ){
Switch (state ){
Case KeyboardListenRelativeLayout. KEYBOARD_STATE_HIDE: // hide the keyboard
EditText. setVisibility (View. VISIBLE );
Break;
Case KeyboardListenRelativeLayout. KEYBOARD_STATE_SHOW: // displayed on the keyboard
EditText. setVisibility (View. GONE );
Break;
Default:
Break;
}
}
});
}
}
Layout file used:

[Html] view plaincopyprint? <? Xml version = "1.0" encoding = "UTF-8"?>
<Com. demo. softkeyboard. KeyboardListenRelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/keyboardRelativeLayout"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<ScrollView android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentTop = "true"
Android: fillViewport = "true">
<LinearLayout android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical">
<EditText android: id = "@ + id/editText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
</ScrollView>
</Com. demo. softkeyboard. KeyboardListenRelativeLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Com. demo. softkeyboard. KeyboardListenRelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/keyboardRelativeLayout"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<ScrollView android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentTop = "true"
Android: fillViewport = "true">
<LinearLayout android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical">
<EditText android: id = "@ + id/editText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
</ScrollView>
</Com. demo. softkeyboard. KeyboardListenRelativeLayout>

Author: qiaoning13256
 

 

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.