android检测软键盘是否弹起

android.view.ViewGroup

protected void onLayout(boolean changed, int l, int t, int r, int b)

执行layout操作时调用onLayout方法。View要给它的每个Child设定size和position。拥有Children的子类需要重写onLayout方法并且调用每个Child的layout方法。

参数changed表示view的size或position发生变化。参数l, t, r, b分别表示相对于parent的left, top, right, bottom position。

 

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

测量View及其Content,确定measuredWidth和measuredHeight。在方法measure(int, int)中调用。重写onMeasure方法时,需要调用方法setMeasuredDimension(int, int),存储View的measuredWidth和measuredHeight。若存储失败,方法measure(int, int)会抛出异常IllegalStateException。可以调用super.onMeasure(int, int)方法。

除非MeasureSpec准许更大的size,否则measure的默认实现是background size。子类重写onMeasure(int, int)提供Content的更佳测量。如果onMeasure被重写,子类必须保证measuredWidth和measuredHeight至少是view的minHeight和minWidth。minHeight/Width通过getSuggestedMinimumHight/Width()获取。

参数width/heightMeasureSpec表示parent强加的horizontal/vertical space要求。

 

void android.view.ViewGroup.layout(int l, int t, int r, int b)

给view及其descendants设定size和position。它正是layout机制的第2个步,每个parent调用它的chlidren的layout操作。使用在measure阶段测量得到的size和position数据完成layout操作。拥有child的子类必须重写onLayout方法,调用每个child的layout操作。

 

void android.view.ViewGroup.measureChildren(int widthMeasureSpec, int heightMeasureSpec)

请View的所有children测量themseles, 测量依据是View的MeasureSpec和padding。忽略处于GONE状态的children,是否GONE状态由getChildMeasureSpec来确定。

参数width/heightMeasureSpec表示view的width/height要求。




使用自定义布局,页面布局中包含ScrollVIew,在软键盘弹起后,布局的高度会发生改变,根据布局的高度来判断软键盘的状态。

  1. package com.ransj.keyboard;

  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.util.Log;
  5. import android.widget.RelativeLayout;

  6. public class KeyboardLayout extends RelativeLayout {
  7. private static final String TAG = KeyboardLayout.class.getSimpleName();
  8. public static final byte KEYBOARD_STATE_SHOW = -3;
  9. public static final byte KEYBOARD_STATE_HIDE = -2;
  10. public static final byte KEYBOARD_STATE_INIT = -1;
  11. private boolean mHasInit;
  12. private boolean mHasKeybord;
  13. private int mHeight;
  14. private onKybdsChangeListener mListener;

  15. public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) {
  16. super(context, attrs, defStyle);
  17. }

  18. public KeyboardLayout(Context context, AttributeSet attrs) {
  19. super(context, attrs);
  20. }

  21. public KeyboardLayout(Context context) {
  22. super(context);
  23. }
  24. /**
  25. * set keyboard state listener
  26. */
  27. public void setOnkbdStateListener(onKybdsChangeListener listener){
  28. mListener = listener;
  29. }
  30. @Override
  31. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  32. super.onLayout(changed, l, t, r, b);
  33. if (!mHasInit) {
  34. mHasInit = true;
  35. mHeight = b;
  36. if (mListener != null) {
  37. mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
  38. }
  39. } else {
  40. mHeight = mHeight < b ? b : mHeight;
  41. }
  42. if (mHasInit && mHeight > b) {
  43. mHasKeybord = true;
  44. if (mListener != null) {
  45. mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
  46. }
  47. Log.w(TAG, "show keyboard.......");
  48. }
  49. if (mHasInit && mHasKeybord && mHeight == b) {
  50. mHasKeybord = false;
  51. if (mListener != null) {
  52. mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
  53. }
  54. Log.w(TAG, "hide keyboard.......");
  55. }
  56. }

  57. public interface onKybdsChangeListener{
  58. public void onKeyBoardStateChange(int state);
  59. }
  60. }
复制代码
这个是自定义的布局,自定义布局可以继承各种常见布局。自定义布局有键盘状态改变监听器,可以通过注册监听器来监听软键盘状态。
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <com.ransj.keyboard.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:id="@+id/keyboardLayout1"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent" >

  6.     <ScrollView
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="fill_parent"
  9.         android:layout_alignParentLeft="true"
  10.         android:layout_alignParentTop="true"
  11.         android:fillViewport="true" >

  12.         <LinearLayout
  13.             android:layout_width="fill_parent"
  14.             android:layout_height="wrap_content"
  15.             android:orientation="vertical" >

  16.             <TextView
  17.                 android:id="@+id/testone_tv"
  18.                 android:layout_width="fill_parent"
  19.                 android:layout_height="wrap_content"
  20.                 android:background="#000000"
  21.                 android:gravity="center"
  22.                 android:text="软件盘弹起,我将消失!软键盘隐藏,我将回来!"
  23.                 android:layout_weight="1.0"
  24.                 android:textColor="#00ff00"
  25.                 android:textStyle="bold" />

  26.             <EditText
  27.                 android:id="@+id/testone_et"
  28.                 android:layout_width="fill_parent"
  29.                 android:layout_height="wrap_content"></EditText>
  30.         </LinearLayout>
  31.     </ScrollView>

  32. </com.ransj.keyboard.KeyboardLayout>
复制代码
这个是优化页面的布局,从上面可以看出自定义布局的用法。
  1. package com.ransj.keyboard;

  2. import com.ransj.keyboard.KeyboardLayout.onKybdsChangeListener;

  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.TextView;
  7. import android.widget.Toast;
  8. /**

  9. *
  10. */
  11. public class TestOne extends Activity{

  12.         @Override
  13.         protected void onCreate(Bundle savedInstanceState) {
  14.                 super.onCreate(savedInstanceState);
  15.                 setContentView(R.layout.testone);
  16.                 KeyboardLayout mainView = (KeyboardLayout) findViewById(R.id.keyboardLayout1);
  17.                 final TextView tv = (TextView) findViewById(R.id.testone_tv);
  18.                 mainView.setOnkbdStateListener(new onKybdsChangeListener() {
  19.                         
  20.                         public void onKeyBoardStateChange(int state) {
  21.                                 switch (state) {
  22.                                 case KeyboardLayout.KEYBOARD_STATE_HIDE:
  23.                                         tv.setVisibility(View.VISIBLE);
  24.                                         Toast.makeText(getApplicationContext(), "软键盘隐藏", Toast.LENGTH_SHORT).show();
  25.                                         break;
  26.                                 case KeyboardLayout.KEYBOARD_STATE_SHOW:
  27.                                         tv.setVisibility(View.INVISIBLE);
  28.                                         Toast.makeText(getApplicationContext(), "软键盘弹起", Toast.LENGTH_SHORT).show();
  29.                                         break;
  30.                                 }
  31.                         }
  32.                 });
  33.         }

  34.         @Override
  35.         protected void onDestroy() {
  36.                 super.onDestroy();
  37.         }

  38.         @Override
  39.         protected void onPause() {
  40.                 super.onPause();
  41.         }

  42. }
复制代码

这个是优化页面的代码逻辑,通过注册监听器,来监听软键盘事件。在软键盘弹起时隐藏Textview,在软键盘隐藏时显示Textview。



msg_send_view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
int total_msg_height=msg_send_view.getRootView().getHeight()-msg_send_view.getHeight();
LogHelper.info("******************************2布局高度:"+currentHeight);//获取当前短信布局高度
LogHelper.info("******************************2屏幕高度:"+globleHeight);//获取当前短信布局高度
LogHelper.info("******************************2适配器高度:"+msgHeight);//获取当前短信布局高度
msgHeight=mPullRefreshListView.getMeasuredHeight();
if(total_msg_height>100){
if(currentHeight>msgHeight){

actualListView.setStackFromBottom(true);
}
else{
actualListView.setStackFromBottom(false);

}
}else{
if(currentHeight>globleHeight){
actualListView.setStackFromBottom(true);
}
else{
actualListView.setStackFromBottom(false);

}
}

}
});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值