Android 安卓向下滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2667471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:51:22  来源:igfitidea点击:

android scroll down

androidscrolltextview

提问by d-man

I have text area and and down to that "ok" and "cancel" button. when i click on text area keyboard appear and focus on the text area and buttons get hide behind the keyboard.

我有文本区域,再到“确定”和“取消”按钮。当我点击文本区域键盘出现并专注于文本区域和按钮隐藏在键盘后面。

i want when text area get focus scroll a little bit and also display the bottom buttons while text area id selected.

我希望当文本区域获得焦点时滚动一点,并在选择文本区域 id 时显示底部按钮。

回答by d-man

i fixed it by myself :D following is the code

我自己修好了 :D 以下是代码

i called the following method when textfield gain the focus

当文本字段获得焦点时,我调用了以下方法

private final void focusOnButtons(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ScrollView sv = (ScrollView)findViewById(R.id.scrl);
                sv.scrollTo(0, sv.getBottom());
            }
        },1000);
    }

回答by Dan B

You could try wrapping your existing layout in a ScrollView:
https://developer.android.com/reference/android/widget/ScrollView.html
Your layout xml file might look like:

您可以尝试将现有布局包装在 ScrollView 中:
https: //developer.android.com/reference/android/widget/ScrollView.html
您的布局 xml 文件可能如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollView01" android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <EditText android:text="@string/hello" android:id="@+id/EditText01" 
            android:layout_width="wrap_content" android:layout_height="wrap_content">
        </EditText>
        <LinearLayout 
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button android:text="@+id/Button01" android:id="@+id/Button01" 
                android:layout_width="wrap_content" android:layout_height="wrap_content">
            </Button>
            <Button android:text="@+id/Button02" android:id="@+id/Button02" 
                android:layout_width="wrap_content" android:layout_height="wrap_content">
            </Button>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

Comment Response:
You might want to check out this page:
https://developer.android.com/training/keyboard-input/index.html

评论回复:
您可能想查看此页面:https:
//developer.android.com/training/keyboard-input/index.html

I think results you want might be accomplished by moving the buttons outside of the Scrollview and setting android:windowSoftInputMode="adjustResize"on the activity.

我认为您想要的结果可以通过将按钮移到 Scrollview 之外并android:windowSoftInputMode="adjustResize"在活动上进行设置来实现。

回答by Mario Velasco

I would adjust few things from the solution:

我会从解决方案中调整一些事情:

1.- Put smoothScroll instead of hard scroll

1.- 使用 smoothScroll 而不是硬滚动

2.- Decrease the delay from 1000 to 300 (enough)

2.- 将延迟从 1000 减少到 300(足够了)

    OnFocusChangeListener onFocus = new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    scrollView.smoothScrollTo(0, scrollView.getBottom());
                }
            },300);
        }
    };