检测软键盘的弹起与隐藏【绝对经典,好用】
2014-08-13 15:39
246 查看
转自:http://www.eoeandroid.com/thread-157446-1-1.html
今天看到社区里面有人问关于如何检测软键盘的弹起和隐藏事件。正确处理好软键盘的弹起和隐藏可以大大提升应用的体验。这一点,“切客优惠”做的很好。
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/232006aci5lrbrihh2crll.png)
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/232008vs1ori451wwswk12.png)
在软键盘弹起后,下面的分享内容自动隐藏,并且在titlebar上,显示签到按钮。这个是一个非常贴心的设计,用户体检大大提升。
这是被大家视为无解的难题,我给大家提供一种方法。
有图为证:
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/224839pgy67ej2qz1j2t6j.png)
初始化页面
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/224838ryxli7haxhaohykv.png)
刀客优化版初始页
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/225209d7d8uk93k5z3kz7q.png)
优化版键盘弹起
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/224836onfo0dd66f62ob0u.png)
软键盘隐藏
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/224835askl522s9s95llat.png)
嘿嘿,未优化版的钉子户
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/224832ikwsws58o5oki4ul.png)
钉子户果然坚挺啊
下面谈谈偶的实现原理:
使用自定义布局,页面布局中包含ScrollVIew,在软键盘弹起后,布局的高度会发生改变,根据布局的高度来判断软键盘的状态。
package com.ransj.keyboard;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;
public class KeyboardLayout extends RelativeLayout {
private static final String TAG = KeyboardLayout.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;
private boolean mHasKeybord;
private int mHeight;
private onKybdsChangeListener mListener;
public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public KeyboardLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardLayout(Context context) {
super(context);
}
/**
* set keyboard state listener
*/
public void setOnkbdStateListener(onKybdsChangeListener listener){
mListener = listener;
}
@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 (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
}
} else {
mHeight = mHeight < b ? b : mHeight;
}
if (mHasInit && mHeight > b) {
mHasKeybord = true;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
}
Log.w(TAG, "show keyboard.......");
}
if (mHasInit && mHasKeybord && mHeight == b) {
mHasKeybord = false;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
}
Log.w(TAG, "hide keyboard.......");
}
}
public interface onKybdsChangeListener{
public void onKeyBoardStateChange(int state);
}
}
复制代码
这个是自定义的布局,自定义布局可以继承各种常见布局。自定义布局有键盘状态改变监听器,可以通过注册监听器来监听软键盘状态。
<?xml version="1.0" encoding="utf-8"?>
<com.ransj.keyboard.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboardLayout1"
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" >
<TextView
android:id="@+id/testone_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
android:text="软件盘弹起,我将消失!软键盘隐藏,我将回来!"
android:layout_weight="1.0"
android:textColor="#00ff00"
android:textStyle="bold" />
<EditText
android:id="@+id/testone_et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
</ScrollView>
</com.ransj.keyboard.KeyboardLayout>
复制代码
这个是优化页面的布局,从上面可以看出自定义布局的用法。
package com.ransj.keyboard;
import com.ransj.keyboard.KeyboardLayout.onKybdsChangeListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
*
*
*/
public class TestOne extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testone);
KeyboardLayout mainView = (KeyboardLayout) findViewById(R.id.keyboardLayout1);
final TextView tv = (TextView) findViewById(R.id.testone_tv);
mainView.setOnkbdStateListener(new onKybdsChangeListener() {
public void onKeyBoardStateChange(int state) {
switch (state) {
case KeyboardLayout.KEYBOARD_STATE_HIDE:
tv.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "软键盘隐藏", Toast.LENGTH_SHORT).show();
break;
case KeyboardLayout.KEYBOARD_STATE_SHOW:
tv.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "软键盘弹起", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
}
}
复制代码
这个是优化页面的代码逻辑,通过注册监听器,来监听软键盘事件。在软键盘弹起时隐藏Textview,在软键盘隐藏时显示Textview。
今天看到社区里面有人问关于如何检测软键盘的弹起和隐藏事件。正确处理好软键盘的弹起和隐藏可以大大提升应用的体验。这一点,“切客优惠”做的很好。
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/232006aci5lrbrihh2crll.png)
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/232008vs1ori451wwswk12.png)
在软键盘弹起后,下面的分享内容自动隐藏,并且在titlebar上,显示签到按钮。这个是一个非常贴心的设计,用户体检大大提升。
这是被大家视为无解的难题,我给大家提供一种方法。
有图为证:
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/224839pgy67ej2qz1j2t6j.png)
初始化页面
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/224838ryxli7haxhaohykv.png)
刀客优化版初始页
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/225209d7d8uk93k5z3kz7q.png)
优化版键盘弹起
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/224836onfo0dd66f62ob0u.png)
软键盘隐藏
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/224835askl522s9s95llat.png)
嘿嘿,未优化版的钉子户
![](http://www.eoeandroid.com/data/attachment/forum/201202/23/224832ikwsws58o5oki4ul.png)
钉子户果然坚挺啊
下面谈谈偶的实现原理:
使用自定义布局,页面布局中包含ScrollVIew,在软键盘弹起后,布局的高度会发生改变,根据布局的高度来判断软键盘的状态。
package com.ransj.keyboard;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;
public class KeyboardLayout extends RelativeLayout {
private static final String TAG = KeyboardLayout.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;
private boolean mHasKeybord;
private int mHeight;
private onKybdsChangeListener mListener;
public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public KeyboardLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardLayout(Context context) {
super(context);
}
/**
* set keyboard state listener
*/
public void setOnkbdStateListener(onKybdsChangeListener listener){
mListener = listener;
}
@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 (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
}
} else {
mHeight = mHeight < b ? b : mHeight;
}
if (mHasInit && mHeight > b) {
mHasKeybord = true;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
}
Log.w(TAG, "show keyboard.......");
}
if (mHasInit && mHasKeybord && mHeight == b) {
mHasKeybord = false;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
}
Log.w(TAG, "hide keyboard.......");
}
}
public interface onKybdsChangeListener{
public void onKeyBoardStateChange(int state);
}
}
复制代码
这个是自定义的布局,自定义布局可以继承各种常见布局。自定义布局有键盘状态改变监听器,可以通过注册监听器来监听软键盘状态。
<?xml version="1.0" encoding="utf-8"?>
<com.ransj.keyboard.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboardLayout1"
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" >
<TextView
android:id="@+id/testone_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
android:text="软件盘弹起,我将消失!软键盘隐藏,我将回来!"
android:layout_weight="1.0"
android:textColor="#00ff00"
android:textStyle="bold" />
<EditText
android:id="@+id/testone_et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
</ScrollView>
</com.ransj.keyboard.KeyboardLayout>
复制代码
这个是优化页面的布局,从上面可以看出自定义布局的用法。
package com.ransj.keyboard;
import com.ransj.keyboard.KeyboardLayout.onKybdsChangeListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
*
*
*/
public class TestOne extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testone);
KeyboardLayout mainView = (KeyboardLayout) findViewById(R.id.keyboardLayout1);
final TextView tv = (TextView) findViewById(R.id.testone_tv);
mainView.setOnkbdStateListener(new onKybdsChangeListener() {
public void onKeyBoardStateChange(int state) {
switch (state) {
case KeyboardLayout.KEYBOARD_STATE_HIDE:
tv.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "软键盘隐藏", Toast.LENGTH_SHORT).show();
break;
case KeyboardLayout.KEYBOARD_STATE_SHOW:
tv.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "软键盘弹起", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
}
}
复制代码
这个是优化页面的代码逻辑,通过注册监听器,来监听软键盘事件。在软键盘弹起时隐藏Textview,在软键盘隐藏时显示Textview。
相关文章推荐
- 检测软键盘的弹起与隐藏【绝对经典,好用】
- 检测软键盘的弹起与隐藏【绝对经典,好用】
- 检测软键盘的弹起与隐藏【绝对经典,好用】 ...
- android 检测软键盘的弹起与隐藏【绝对经典,好用】
- 检测软键盘的弹起与隐藏
- Android检测软键盘的弹起与隐藏
- Android 检测软键盘的弹起与隐藏
- 检测软键盘的弹起与隐藏
- Android监听软键盘的弹起和隐藏
- android 监听软键盘弹起和关闭状态与点击空白区域软键盘隐藏
- 随软键盘的弹起与隐藏,界面做合理的调整的一种实现(避免软键盘遮挡内容的显示)
- Android检测软键盘显示状态,隐藏软件键盘
- iOS软键盘当被遮挡时,自动弹起,点击return时隐藏软键盘,点击外部UIView时,隐藏软键盘
- 监听软键盘的弹起与隐藏
- android检测软键盘是否弹起
- Android 软键盘弹起隐藏的监听
- Android中点击空白处隐藏软键盘
- Android,隐藏/显示软键盘
- 判断软键盘,显示或隐藏
- Android界面隐藏软键盘的探索(兼findViewById返回null解决办法)