android 解決輸入法鍵盤遮蓋布局問題


 

    /**
     * @param root 最外層布局,需要調整的布局
     * @param scrollToView 被鍵盤遮擋的scrollToView,滾動root,使scrollToView在root可視區域的底部
     */
    private void controlKeyboardLayout(final View root, final View scrollToView) {
        root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                //獲取root在窗體的可視區域
                root.getWindowVisibleDisplayFrame(rect);
                //獲取root在窗體的不可視區域高度(被其他View遮擋的區域高度)
                int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
                //若不可視區域高度大於100,則鍵盤顯示
                if (rootInvisibleHeight > 100) {
                    int[] location = new int[2];
                    //獲取scrollToView在窗體的坐標
                    scrollToView.getLocationInWindow(location);
                    //計算root滾動高度,使scrollToView在可見區域的底部
                    int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
                    root.scrollTo(0, srollHeight);
                } else {
                    //鍵盤隱藏
                    root.scrollTo(0, 0);
                }
            }
        });
    }

 

效果圖如下:

 

 

下面提供完整的代碼及布局文件:

1. MainActivity

public class MainActivity extends Activity {
    
    private LinearLayout mRoot;
    private Button mSubmit;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRoot = (LinearLayout) findViewById(R.id.root);
        mSubmit = (Button) findViewById(R.id.submit);
        controlKeyboardLayout(mRoot, mSubmit);
    }
    
    /**
     * @param root 最外層布局,需要調整的布局
     * @param scrollToView 被鍵盤遮擋的scrollToView,滾動root,使scrollToView在root可視區域的底部
     */
    private void controlKeyboardLayout(final View root, final View scrollToView) {
        root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                //獲取root在窗體的可視區域
                root.getWindowVisibleDisplayFrame(rect);
                //獲取root在窗體的不可視區域高度(被其他View遮擋的區域高度)
                int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
                //若不可視區域高度大於100,則鍵盤顯示
                if (rootInvisibleHeight > 100) {
                    int[] location = new int[2];
                    //獲取scrollToView在窗體的坐標
                    scrollToView.getLocationInWindow(location);
                    //計算root滾動高度,使scrollToView在可見區域
                    int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
                    root.scrollTo(0, srollHeight);
                } else {
                    //鍵盤隱藏
                    root.scrollTo(0, 0);
                }
            }
        });
    }

}

 

2. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_vertical" >
    
    <EditText android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:hint="edit1"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:hint="edit2"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:hint="edit3"/>
    <Button android:id="@+id/submit"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:text="submit"/>

</LinearLayout>


免責聲明!

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



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