EditText+边框布局之软键盘适配

问题点;

  • 在给EditText外加一个边框的情况弹出键盘,输入框以下部分会被隐盖
    解决思路以及方案:

  • 思路
    如果我能够计算出当前界面的可见区域的高度和和软键盘弹出的高度,此问题是否就能迎刃而解呢,答案是肯定,

1.实现代码


     import android.graphics.Rect;
   import android.util.Log;
    import android.view.View; 
    import android.view.ViewTreeObserver;
    import java.util.LinkedList; 
    import java.util.List;
   
   
   public class SoftKeyBroadManager implements
   ViewTreeObserver.OnGlobalLayoutListener{
   
       public interface SoftKeyboardStateListener {
           void onSoftKeyboardOpened(int keyboardHeightInPx);
   
           void onSoftKeyboardClosed();
       }
   
       private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();
       private final View activityRootView;
       private int lastSoftKeyboardHeightInPx;
       private boolean isSoftKeyboardOpened;
   
       public SoftKeyBroadManager(View activityRootView) {
           this(activityRootView, false);
       }
   
       public SoftKeyBroadManager(View activityRootView, boolean isSoftKeyboardOpened) {
           this.activityRootView = activityRootView;
           this.isSoftKeyboardOpened = isSoftKeyboardOpened;
           activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
   
           
       }
   
       @Override
       public void onGlobalLayout() {
           final Rect r = new Rect();
           //r will be populated with the coordinates of your view that area still visible.
           activityRootView.getWindowVisibleDisplayFrame(r);
           final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
           Log.d("SoftKeyboardStateHelper", "heightDiff:" + heightDiff);
           if (!isSoftKeyboardOpened && heightDiff > 500) { // if more than 100 pixels, its probably a keyboard...
               isSoftKeyboardOpened = true;
               notifyOnSoftKeyboardOpened(heightDiff);
               //if (isSoftKeyboardOpened && heightDiff < 100)
           } else if (isSoftKeyboardOpened && heightDiff < 500) {
               isSoftKeyboardOpened = false;
               notifyOnSoftKeyboardClosed();
           }
       }
   
       public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
           this.isSoftKeyboardOpened = isSoftKeyboardOpened;
       }
   
       public boolean isSoftKeyboardOpened() {
           return isSoftKeyboardOpened;
       }
   
       /**
        * Default value is zero (0)
        *
        * @return last saved keyboard height in px
        */
       public int getLastSoftKeyboardHeightInPx() {
           return lastSoftKeyboardHeightInPx;
       }
   
       public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
           listeners.add(listener);
       }
   
       public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
           listeners.remove(listener);
       }
   
       private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
           this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;
   
           for (SoftKeyboardStateListener listener : listeners) {
               if (listener != null) {
                   listener.onSoftKeyboardOpened(keyboardHeightInPx);
               }
           }
       }
   
       private void notifyOnSoftKeyboardClosed() {
           for (SoftKeyboardStateListener listener : listeners) {
               if (listener != null) {
                   listener.onSoftKeyboardClosed();
               }
           }
       } }

  • 代码中主要的算法部分解析
   final Rect r = new Rect();
        //r will be populated with the coordinates of your view that area still visible.
        activityRootView.getWindowVisibleDisplayFrame(r);
        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        Log.d("SoftKeyboardStateHelper", "heightDiff:" + heightDiff);
        if (!isSoftKeyboardOpened && heightDiff > 500) { // if more than 100 pixels, its probably a keyboard...
            isSoftKeyboardOpened = true;
            notifyOnSoftKeyboardOpened(heightDiff);
            //if (isSoftKeyboardOpened && heightDiff < 100)
        } else if (isSoftKeyboardOpened && heightDiff < 500) {
            isSoftKeyboardOpened = false;
            notifyOnSoftKeyboardClosed();
        }
  • 首先是判断软键盘是否弹出,

  • 通过以下的代码段获取可是区域的高度,没错就这么简单,是不是尼玛有点怀疑人生了,小老弟淡定吧,赶紧安静的搬砖


activityRootView.getWindowVisibleDisplayFrame(r);
        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
  • 如何使用,
SoftKeyBroadManager softKeyBroadManager=new SoftKeyBroadManager(linearLayout);       
//注册监听接口
 softKeyBroadManager.addSoftKeyboardStateListener(this); 
 //注销监听接口
 softKeyBroadManager.removeSoftKeyboardStateListener(this);
  • 接口实现
@Override
    public void onSoftKeyboardOpened(int keyboardHeightInPx) {
        linearLayout.setTranslationY(-keyboardHeightInPx+getStatusBarHeight(LoginActivity.this));
    }


    @Override
    public void onSoftKeyboardClosed() {
        linearLayout.setTranslationY(0);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

将哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值