Android监听软键盘的弹起和隐藏
2015-12-02 19:00
549 查看
做安卓键盘功能的时候,要是需求不单单是输入文字,比如要输入表情,图片等的时候,就需要监听键盘的弹起和隐藏。在实际的项目中也遇到了这样的需求。接下来就写个博客,记录一些方法。
实现原理就是监听自定义控件的高度变换得键盘的弹起和隐藏状态。
都是程序员都心急。直接上代码:
MainActivity:
activity_main.xml:
ListenerInputView:
看到这里的朋友明白了,其实很简单。战友们我们一起加油!
原创链接:http://blog.csdn.net/u010052279/article/details/50151177
最后附上demo下载地址:http://download.csdn.net/detail/u010052279/9320537
实现原理就是监听自定义控件的高度变换得键盘的弹起和隐藏状态。
都是程序员都心急。直接上代码:
MainActivity:
package com.example.testsoftinput; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; import com.example.testsoftinput.ListenerInputView.OnKeyBoardStateChangeListener; public class MainActivity extends Activity { private ListenerInputView mListenerInputView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListenerInputView=(ListenerInputView) findViewById(R.id.mListenerInputView); mListenerInputView.setOnKeyBoardStateChangeListener(new OnKeyBoardStateChangeListener() { @Override public void OnKeyBoardState(int state) { // TODO Auto-generated method stub switch (state) { //开启 case 1: Toast.makeText(getApplicationContext(), "输入法显示了.", 1).show(); break; //关闭 case 0: Toast.makeText(getApplicationContext(), "变化为正常状态(输入法关闭).", 1).show(); break; default: break; } } }); } }
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}" > <com.example.testsoftinput.ListenerInputView android:id="@+id/mListenerInputView" android:layout_width="2dip" android:layout_height="match_parent" > </com.example.testsoftinput.ListenerInputView> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#22ff0000" android:gravity="center" android:text="Android技术交流群 :110520459" /> <EditText android:layout_width="match_parent" android:layout_height="50dip" android:hint="监听软键盘的弹起和隐藏" /> </RelativeLayout>
ListenerInputView:
package com.example.testsoftinput; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; public class ListenerInputView extends View { private static final boolean DEBUG = true; private static final String TAG = "softinput"; private Context mContext; private int softKeyboardHeight = 0; public ListenerInputView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public ListenerInputView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public ListenerInputView(Context context) { super(context); init(context); } private void init(Context context) { this.mContext = context; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if (DEBUG) { Log.i(TAG, "screenHeight=" + 0 + ";w=" + w + ";h=" + h + ";oldw=" + oldw + ";oldh=" + oldh); } if (Math.abs(h - oldh) > softKeyboardHeight) { if (h >= oldh) { if (DEBUG) { if (listener != null) { listener.OnKeyBoardState(0); } Log.i(TAG, "变化为正常状态(输入法关闭)."); } } else { if (DEBUG) { if (listener != null) { listener.OnKeyBoardState(1); } Log.i(TAG, "输入法显示了."); } } } else { if (oldh != 0) { if (DEBUG) { Log.i(TAG, "变化为正常状态.(全屏关闭)"); if (listener != null) { listener.OnKeyBoardState(0); } } } else { if (DEBUG) { Log.i(TAG, "初始化,当前为正常状态."); } } } } /** * 监听键盘变化 state 1 开启软键盘 0关闭软键盘 * * @author haoweilai * */ public interface OnKeyBoardStateChangeListener { void OnKeyBoardState(int state); } private OnKeyBoardStateChangeListener listener; public void setOnKeyBoardStateChangeListener( OnKeyBoardStateChangeListener listener) { this.listener = listener; } }
看到这里的朋友明白了,其实很简单。战友们我们一起加油!
原创链接:http://blog.csdn.net/u010052279/article/details/50151177
最后附上demo下载地址:http://download.csdn.net/detail/u010052279/9320537
相关文章推荐
- [Android]Android系统启动流程源码分析
- 如何修改Android应用程序的图标和名字? .
- Android开发案例 - 自定义虚拟键盘
- Android SDK 开发范例大全 ---3.10不同Activity之间的数据传递
- android-service服务
- Android开发笔记(八)神奇的shape
- 安卓模拟器 com.android.phone已停止运行的解决办法
- Android 换肤方案(2种)
- android 自定义多选dialog(倒圆角)
- 初识Android Project Butter: 黄油项目,使动画更加的平滑、流畅
- Android 4.4 不休眠+不锁屏+默认中文+去除导航栏
- Windows环境下Android Studio系列3—简单设置
- 自定义view————Android上的劳力士
- Android 进程和线程 --多进程
- android Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)
- Windows环境下Android Studio系列3—简单设置
- Android 自定义View (一)
- EditText设置cursor位置
- PullToRefreshListView 如何返回顶部
- Android4.4设置状态栏为透明