android輸入框顯示在軟鍵盤上邊


有時候在界面需要輸入的時候,如果輸入框在界面的下方,軟鍵盤彈出的時候會遮擋輸入框界面,對用戶的體驗不是很好。

在網上找的別人的解決方案

首先:

 清單文件里面配置:
android:windowSoftInputMode="adjustPan|stateHidden"
android:windowSoftInputMode="adjustResize|adjustUnspecified|stateHidden"

 

在需要組件上移的Activity 加入以下代碼:

 View decorView = getWindow().getDecorView();
 View contentView = findViewById(Window.ID_ANDROID_CONTENT);// 此處的控件ID可以使用界面當中的指定的任意控件
  decorView.getViewTreeObserver().addOnGlobalLayoutListener(getGlobalLayoutListener(decorView, contentView));

 

 

private ViewTreeObserver.OnGlobalLayoutListener getGlobalLayoutListener(final View decorView, final View contentView) {
    return new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            decorView.getWindowVisibleDisplayFrame(r);

            int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
            int diff = height - r.bottom;

            if (diff != 0) {
                if (contentView.getPaddingBottom() != diff) {
                    contentView.setPadding(0, 0, 0, diff);
                }
            } else {
                if (contentView.getPaddingBottom() != 0) {
                    contentView.setPadding(0, 0, 0, 0);
                }
            }
        }
    };
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM