Android 判断软键盘的状态(显示,隐藏)
2012-06-29 16:00
489 查看
先上截图,有图有真相:
自定义RelativeLayout
用法:
使用到的布局文件:
源代码:http://download.csdn.net/detail/qiaoning13256/4401259
自定义RelativeLayout
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 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; /** * 软键盘监听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://软键盘隐藏 editText.setVisibility(View.VISIBLE); break; case KeyboardListenRelativeLayout.KEYBOARD_STATE_SHOW://软键盘显示 editText.setVisibility(View.GONE); break; default: break; } } }); } }
使用到的布局文件:
<?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>
源代码:http://download.csdn.net/detail/qiaoning13256/4401259
相关文章推荐
- Android 判断软键盘的状态(显示,隐藏)
- Android判断软键盘显示状态并隐藏
- Android 控制软键盘显示、隐藏,并且添加软键盘的状态监听的终极解决办法
- Android检测软键盘显示状态,隐藏软件键盘
- android输入法属性使用,软键盘隐藏、显示状态控制大揭秘
- android 怎么判断自定义的控件的显示状态(隐藏、显示)
- android 监听判断软键盘的弹出、隐藏状态
- 判断控件的状态是显示(SW_SHOW)还是隐藏(SW_HIDE)
- android 软键盘的显示和隐藏方法
- android软键盘的显示后隐藏
- android 软键盘的显示与隐藏问题的研究
- android软键盘的显示后隐藏
- Android 自定义RelativeLayout活动主窗口大小改变 得到软键盘的显示和隐藏
- 判断模块当前状态是显示还是隐藏
- android 隐藏显示软键盘
- 解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法
- Android软键盘的显示与隐藏
- Android软键盘的隐藏显示研究
- 解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法
- android隐藏以及显示软键盘