键盘出现时向上滚动屏幕

时间:2013-10-24 00:45:33

标签: android scroll keyboard android-edittext

我在中间有一个editText的活动。当我点击editText时,键盘出现并且屏幕移动,以便editText位于键盘上方,覆盖下面的其他内容。

我不想调整大小或为布局添加填充。我只需要屏幕滚动到顶部我需要看到下面的其他东西的空间量。换句话说,我想在键盘出现时选择editText的位置。如果屏幕完全向上滚动,向我显示布局的底部也足够了。

我尝试将布局放在scrollView中,在editText上添加此代码

    editText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //make the view scroll down to the bottom
            scrollView.scrollTo(0, scrollView.getBottom());

        }
    });

但它不起作用。有没有人有想法?提前谢谢!

9 个答案:

答案 0 :(得分:27)

使用 android:windowSoftInputMode =“adjustPan”

<强> “adjustResize”

活动的主窗口始终调整大小,以便为屏幕上的软键盘腾出空间。

<强> “adjustPan”

活动的主窗口未调整大小以便为软键盘腾出空间。相反,窗口的内容会自动平移,以便键盘不会遮挡当前焦点,用户可以随时看到他们正在键入的内容。这通常不如调整大小,因为用户可能需要关闭软键盘才能进入并与窗口的模糊部分进行交互。

例如,您的清单文件: -

       <activity
    android:windowSoftInputMode="adjustPan"
    android:name=".MainActivity"></activity>

迟到的答案。但这肯定对任何人都有帮助。

答案 1 :(得分:16)

我有一次类似的问题,我决定使用:

    android:windowSoftInputMode="adjustResize"

在AndroidManifest.xml中。

如果你使用RelativeLayout开头的底部元素必须挂钩到父底部,然后将另一个元素挂钩到前一个元素之上。

如果您使用LinearLayout将其封装在RelativeLayout中并将LinearLayout挂钩到parentBottom。

这样,当键盘弹出时,您将看到布局的底部。

希望这有帮助。

答案 2 :(得分:8)

我有这个问题,我通过这段代码解决了:

myEdittext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    myscrollview.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            View lastChild = myscrollview.getChildAt(myscrollview.getChildCount() - 1);
                            int bottom = lastChild.getBottom() + myscrollview.getPaddingBottom();
                            int sy = myscrollview.getScrollY();
                            int sh = myscrollview.getHeight();
                            int delta = bottom - (sy + sh);
                            myscrollview.smoothScrollBy(0, delta);
                        }
                    }, 200);
                }
            });

答案 3 :(得分:5)

这对我有用

android:windowSoftInputMode="stateVisible|adjustResize"

在androidmanifest中进行活动

答案 4 :(得分:1)

将此library添加到gradle依赖项中:

KeyboardVisibilityEvent.setEventListener(getActivity(), new KeyboardVisibilityEventListener() {
    @Override
    public void onVisibilityChanged(boolean isOpen) {

        if (isOpen && MY_EDIT_TEXT.hasFocus()) {

            //scroll to last view
            View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1);
            int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom();
            int sy = scrollLayout.getScrollY();
            int sh = scrollLayout.getHeight();
            int delta = bottom - (sy + sh);

            scrollLayout.smoothScrollBy(0, delta);
        }
    }
});

并使用以下代码:

select A.value, B.value
from your_table A
cross join your_table B
where A.value = B.value
order by A.value;

答案 5 :(得分:1)

这有效:-

约束布局中打开键盘时,向上滚动存在问题。 不要使用它。

实现“出现键盘时向上滚动屏幕” 试试这个

使用线性布局 ScrollView

和fill_parent在“滚动”视图中。 以及滚动视图中的任何“文本”字段。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:gravity="center">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">


</ScrollView>
</LinearLayout>

答案 6 :(得分:1)

只需添加

android:screenReaderFocusable="true"

到您的XML文件,它将像超级按钮一样工作

答案 7 :(得分:0)

It only worked for me after I changed from Constraint Layout to Linear Layout and added

android:windowSoftInputMode="adjustResize".

and removed

<item name="android:windowFullscreen">true</item> from the Activity style

答案 8 :(得分:0)

在 Kotlin 中执行此操作的简单方法(从用户 user6581344 的答案中重用),我使用它在设置焦点并显示键盘时手动滚动到 EditText,根据需要额外添加小键盘和 EditText 之间的底部填充:

    binding.scrollView.postDelayed(object:Runnable {

        override fun run() {

            val extraBottomPadding = 100

            val manualScroll = txtFullName.y + extraBottomPadding

            scrollView.smoothScrollBy(0, manualScroll.toInt())
        }

    }, 200)
相关问题