我们经常调用软键盘的谈起与收回,很少监听它的谈起与收回,现在我们一起来看一下我是怎么监听他谈起与收回的代码详解往下看---》
(1)首先我们随便来一个布局文件,越简单越好,就一个输入框其实他也不用写,重点是最外层RelativeLayout给一个id="@+id/super_layout_id"
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/super_layout_id" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="@dimen/tele_item_height" android:layout_marginLeft="17.5dp" android:background="@color/white" android:hint="@string/nopassword_again" android:inputType="textPassword" android:textColorHint="#cccccc" android:maxLength="18" android:textSize="15sp" /> </RelativeLayout> (2) 然后在Activity中实现OnLayoutChangListener接口重写onLayoutChange()方法, layout大小发生改变时监听 ,还有对象.addOnLayoutChangeListerer(this)
下面看代码:
@Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { // TODO Auto-generated method stub //old是改变前的左上右下坐标点值,没有old的是改变后的左上右下坐标点值 <pre name="code" class="java"> //获取屏幕高度 int[] WidthAndHeight = Device.getScreenWidthAndHeight(this); //阀值设置为屏幕高度的1/3 keyHeight = WidthAndHeight[1] / 3; //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高,就认为软键盘弹起 if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {// Toast.makeText(this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show(); } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {// Toast.makeText(this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show(); } } (3)在AndroidManifest里面配置一下代码
<activity android:name=".presentation.view.personcenter.RegistInfoActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:windowSoftInputMode="stateAlwaysHidden|adjustResize"></activity> <activity