鸿 网 互 联 www.68idc.cn

当前位置 : 主页 > 网络程序脚本 > linux shell > >

Android判断软键盘的状态(显示,隐藏)

来源:互联网 作者:佚名 时间:2015-06-03 08:52
先上截图 有图有真相 自定义RelativeLayout package com demo softkeyboard; import ntent Context; import android util AttributeSet; import android widget RelativeLayout; public class KeyboardListenRelativeLayout extends RelativeLayout{ private

  先上截图有图有真相

  

  

  自定义RelativeLayout

  

package comdemosoftkeyboard;  

import ntentContext;  
import androidutilAttributeSet;  
import androidwidgetRelativeLayout;  

publicclass KeyboardListenRelativeLayout extends RelativeLayout {  

privatestaticfinal String TAG = KeyboardListenRelativeLayoutclassgetSimpleName();  

publicstaticfinalbyte KEYBOARD_STATE_SHOW = ;  
publicstaticfinalbyte KEYBOARD_STATE_HIDE = ;  
publicstaticfinalbyte KEYBOARD_STATE_INIT = ;  

privateboolean mHasInit = false;  
privateboolean mHasKeyboard = false;  
privateint 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);  
    }  

publicvoid setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) {  
thisonKeyboardStateChangedListener = onKeyboardStateChangedListener;  
    }  

@Override
protectedvoid onLayout(boolean changed int l int t int r int b) {  
superonLayout(changed l t r b);  
if(!mHasInit) {  
            mHasInit = true;  
            mHeight = b;  
if(onKeyboardStateChangedListener != null) {  
                onKeyboardStateChangedListeneronKeyboardStateChanged(KEYBOARD_STATE_INIT);  
            }  
        } else {  
            mHeight = mHeight < b ? b : mHeight;  
        }  

if(mHasInit && mHeight > b) {  
            mHasKeyboard = true;  
if(onKeyboardStateChangedListener != null) {  
                onKeyboardStateChangedListeneronKeyboardStateChanged(KEYBOARD_STATE_SHOW);  
            }  
        }  
if(mHasInit && mHasKeyboard && mHeight == b) {  
            mHasKeyboard = false;  
if(onKeyboardStateChangedListener != null) {  
                onKeyboardStateChangedListeneronKeyboardStateChanged(KEYBOARD_STATE_HIDE);  
            }  
        }  
    }  

publicinterface IOnKeyboardStateChangedListener {  
publicvoid onKeyboardStateChanged(int state);  
    }  
}  

用法

  

package comdemosoftkeyboard;  

import comdemosoftkeyboardKeyboardListenRelativeLayoutIOnKeyboardStateChangedListener;  

import androidappActivity;  
import androidosBundle;  
import androidviewView;  
import androidwidgetEditText;  
import androidwidgetTextView;  

/**
 * 软键盘监听Demo
 * @author qiaoning
 *
 */
publicclass SoftKeyboardListenDemoActivity extends Activity {  

private EditText editText;  
    KeyboardListenRelativeLayout relativeLayout;  

@Override
publicvoid onCreate(Bundle savedInstanceState) {  
superonCreate(savedInstanceState);  
        setContentView(Rlayoutmain);  

        relativeLayout = (KeyboardListenRelativeLayout) findViewById(RidkeyboardRelativeLayout);  
        editText = (EditText) findViewById(RideditText);  

        relativeLayoutsetOnKeyboardStateChangedListener(new IOnKeyboardStateChangedListener() {  

publicvoid onKeyboardStateChanged(int state) {  
switch (state) {  
case KeyboardListenRelativeLayoutKEYBOARD_STATE_HIDE://软键盘隐藏
                    editTextsetVisibility(ViewVISIBLE);  
break;  
case KeyboardListenRelativeLayoutKEYBOARD_STATE_SHOW://软键盘显示
                    editTextsetVisibility(ViewGONE);  
break;  
default:  
break;  
                }  
            }  
        });  
    }  
}  

使用到的布局文件

  

<?xmlversion=encoding=utf?>
<comdemosoftkeyboardKeyboardListenRelativeLayoutxmlns:android=
android:id=@+id/keyboardRelativeLayout
android:layout_width=fill_parent
android:layout_height=fill_parent>
<ScrollViewandroid:layout_width=fill_parent
android:layout_height=fill_parent
android:layout_alignParentLeft=true
android:layout_alignParentTop=true
android:fillViewport=true>
<LinearLayoutandroid:layout_width=fill_parent
android:layout_height=wrap_content
android:orientation=vertical>
<EditTextandroid:id=@+id/editText
android:layout_width=fill_parent
android:layout_height=wrap_content/>
</LinearLayout>
</ScrollView>
</comdemosoftkeyboardKeyboardListenRelativeLayout>

  源码下载地址

  免费下载地址在

  用户名与密码都是

  具体下载目录在 /年资料/月/日/Android 判断软键盘的状态(显示隐藏)

网友评论