关闭

Android全屏下输入法软键盘遇到的问题

673人阅读 评论(4) 收藏 举报
分类:

Android全屏下输入法软键盘遇到的问题

最近在项目中遇到了在全屏模式下输入法遮挡EditText的问题. 到目前只是暂时解决, 还没有完美解决.

看了很多博主的文章, 提到过很到方法大部分方法无效.
大部分的文章讲述的方法都是在非全屏下的可用的,但是到了全屏模式下就无效了.

从各位大神的博文中可以得到如下结论.

在全屏模式下onSizeChanged()方法不会被调用.
设置windowInputSoftMethod=”adjustResize”无效.
用ScrollView包裹需滚动的内容也无效.

2016/06/03补充:
经过我阅读很多文章发现: 全屏下输入法弹出布局不调整导致的遮盖问题, 这是android系统本来就有的问题 . 如果不设置为全屏, 或者在content区域使用android:fitsSystemWindows=”true”让内容区不扩展到状态栏, 这样使用adjustResize可以让输入法不遮挡控件, 当然, 前提是你的布局可以滚动. 这样的话, 你要透明化状态栏,将状态栏设置成和你内容区一样的颜色, 让它看起来像是全屏.

目前看到国内写得最好的文章
1. http://blog.csdn.net/a2758963/article/details/25163171
2. http://blog.csdn.net/a2758963/article/details/25280507
3. http://blog.csdn.net/mynameishuangshuai/article/details/51567357#t9

最后使用的也是第2篇文章中提到的AndroidBug5497Workaround开源代码的修改版. 但是效果不是很让人满意.

代码来自StackOverFlow的一个visionix的回答:

I implemented Joseph Johnson solution and it worked well, I noticed after using this solution sometimes the drawer on the application will not close properly. I added a functionality to remove the listener removeOnGlobalLayoutListener when the user closes the fragment where are edittexts located.

//when the application uses full screen theme and the keyboard is shown the content not scrollable! 
//with this util it will be scrollable once again
//http://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible
public class AndroidBug5497Workaround {


    private static AndroidBug5497Workaround mInstance = null;
    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;
    private ViewTreeObserver.OnGlobalLayoutListener _globalListener;

    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    public static AndroidBug5497Workaround getInstance (Activity activity) {
        if(mInstance==null)
        {
            synchronized (AndroidBug5497Workaround.class)
            {
                mInstance = new AndroidBug5497Workaround(activity);
            }
        }
        return mInstance;
    }

    private AndroidBug5497Workaround(Activity activity) {
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();

        _globalListener = new ViewTreeObserver.OnGlobalLayoutListener()
        {

            @Override
            public void onGlobalLayout()
            {
                 possiblyResizeChildOfContent();
            }
        };
    }

    public void setListener()
    {
         mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(_globalListener);
    }

    public void removeListener()
    {
         mChildOfContent.getViewTreeObserver().removeOnGlobalLayoutListener(_globalListener);
    }

    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);
    } 
}
  • uses the class where is my edittexts located
@Override
public void onStart()
{
    super.onStart();
    AndroidBug5497Workaround.getInstance(getActivity()).setListener();
}

@Override
public void onStop()
{
    super.onStop();
    AndroidBug5497Workaround.getInstance(getActivity()).removeListener();
}

参考资料:
AndroidBug5497Workaround开源代码的github地址

Android How to adjust layout in Full Screen Mode when softkeyboard is visible

How do I Detect if Software Keyboard is Visible on Android Device?

Hiding Title in a Fullscreen mode?

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:54931次
    • 积分:1061
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:16篇
    • 译文:0篇
    • 评论:55条
    高手的博客
    最新评论