软键盘弹出后的布局上移处理

本文介绍如何在多个EditText中管理软键盘弹出,通过监听ViewTreeObserver,当软键盘出现时,通过setTranslationY调整根布局,避免遮挡。提供了软键盘监听管理类的实现和使用示例,适用于平板设备场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在EditText 比较多的情况下,为了避免软键盘弹出,遮盖部分EditText;特别对于平板这种设备时,可以通过以下方式将根布局整体setTranslationY进行上移,软键盘消失时再恢复。

软键盘监听管理类:

public class SoftKeyBroadManager  implements ViewTreeObserver.OnGlobalLayoutListener{

    public interface SoftKeyboardStateListener {
        void onSoftKeyboardOpened(int keyboardHeightInPx);

        void onSoftKeyboardClosed();
    }

    private final List<SoftKeyboardStateListener> listeners = new LinkedList<>();
    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);
        LogUtils.d("SoftKeyBroadManager--onGlobalLayout heightDiff:" + heightDiff);
        if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
            isSoftKeyboardOpened = true;
            notifyOnSoftKeyboardOpened(heightDiff);
        } else if (isSoftKeyboardOpened && heightDiff < 100) {
            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();
            }
        }
    }
}

使用方式:

public class XXXFragment extends Fragment implements View.OnClickListener, SoftKeyBroadManager.SoftKeyboardStateListener {

    View mRootView;
    private SoftKeyBroadManager mSoftKeyBroadManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        mRootView = inflater.inflate(R.layout.XXX, null);
        initView();
        return mRootView;
    }

    private void initView() {
        mSoftKeyBroadManager = new SoftKeyBroadManager(mRootView);
        mSoftKeyBroadManager.addSoftKeyboardStateListener(this);
    }

    @Override
    public void onClick(View v) {
      
    }

    @Override
    public void onPause() {
        super.onPause();
        View focus = mRootView.findFocus();
        LogUtils.d("NewAddressBoxGuideFragment--onPause focus="+focus);
        if (focus != null) {
            ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(focus.getWindowToken(), HIDE_NOT_ALWAYS);
        }
    }

   

    @Override
    public void onSoftKeyboardOpened(int keyboardHeightInPx) {
        mRootView.setTranslationY(-250);
    }

    @Override
    public void onSoftKeyboardClosed() {
        mRootView.setTranslationY(0);
    }

    @Override
    public void onDestroyView() {
        mSoftKeyBroadManager.removeSoftKeyboardStateListener(this);
        super.onDestroyView();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值