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