Android - EditText 与软键盘重叠

2024-01-15

我正在开发一个有一些 EditText 的 Activity。当我单击/触摸 EditText 时,会出现软键盘。但是屏幕底部的 EditText 与软键盘重叠。显示 EditText 的上半部分,下半部分位于键盘下方。

我设置了android:windowSoftInputMode="adjustPan"在AndroidManifest.xml中

关于如何避免它有什么建议吗?


对我来说,我不想假设键盘高度是一定的尺寸。无论您关心什么视图,都创建一个 onTouchListener,然后执行以下操作:

    setOnTouchListener(new OnTouchListener()  {



        Runnable shifter=new Runnable(){
            public void run(){
                try {
                    int[] loc = new int[2];                 
                    //get the location of someview which gets stored in loc array
                    findViewById(R.id.someview).getLocationInWindow(loc);
                    //shift so user can see someview
                    myscrollView.scrollTo(loc[0], loc[1]);   
                }
                catch (Exception e) {
                    e.printStackTrace();
                }   
            }}
        };

        Rect scrollBounds = new Rect();
        View divider=findViewById(R.id.someview);
        myscollView.getHitRect(scrollBounds);
        if (!divider.getLocalVisibleRect(scrollBounds))  {
            // the divider view is NOT  within the visible scroll window thus we need to scroll a bit.
            myscollView.postDelayed(shifter, 500);
        }



    });

//本质上,我们创建一个可运行的对象,滚动到您希望在屏幕上可见的某个视图的新位置。仅当该可运行程序不在滚动视图范围内(不在屏幕上)时才执行该可运行程序。这样,它将滚动视图转移到引用的视图(在我的例子中是“someview”,它是一个行分隔符)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android - EditText 与软键盘重叠 的相关文章