Android EditText输入框被键盘遮挡问题解决

Android EditText输入框被键盘遮挡问题解决:

纯属做笔记:

方法:一

1.在onCreate中加上:

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

2.布局:




    

    

        

            

                

                    
                

                

                    
                
            
            
            

                

                

                

                    
                
            

            

                

                    

                        

                        
                    

                    

                        

                        
                    

                
            

            
            

            
            

                
            
            

            

            
            

                

                

                    

                    

                    

                

            

            

            

这里我在ScrollView中添加了:

android:fillViewport="true"

3.清单文件中设置:

 

方法:二

1.全屏模式下,即使将activity的windowSoftInputMode的属性设置为:adjustResize,在键盘显示时它未将Activity的Screen向上推动

使用方法 :
在你的Activity的oncreate()方法里调用AndroidBug5497Workaround.assistActivity(this);即可。 
注意:在setContentView(R.layout.xxx)之后调用。

在AndroidManifest.xml


        

2.自定义解决方法类:

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
 
public class AndroidBug5497Workaround {
 
 
    public static void assistActivity (Activity activity) {
        new AndroidBug5497Workaround(activity);
    }
 
    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;
 
    private AndroidBug5497Workaround(Activity activity) {
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }
 
    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard/4)) {
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }
 
    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }
 
}

 

你可能感兴趣的:(Android常用的技术)