【问题标题】:Header causing scrollview to not scroll to edit text when keyboard appears出现键盘时导致滚动视图无法滚动以编辑文本的标题
【发布时间】:2015-07-25 00:05:59
【问题描述】:

我想要的是,当用户单击其中一个编辑文本以调整屏幕大小并向上滚动时,它会专注于该编辑文本。

清单已设置为调整大小:

  <activity
     android:name=".ui.login.LoginActivity"
     android:label="@string/app_name"
     android:theme="@style/AppThemes"
     android:windowSoftInputMode="stateHidden|adjustResize"/>

登录屏幕的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/main_background" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!--<LinearLayout-->
                <!--android:id="@+id/login_header"-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:paddingTop="30dp"-->
                <!--android:paddingBottom="30dp"-->
                <!--android:gravity="center_horizontal"-->
                <!--android:orientation="vertical">-->

                <!--<ImageView-->
                    <!--android:id="@+id/login_fragment_logo"-->
                    <!--android:layout_width="120dp"-->
                    <!--android:layout_height="120dp"-->
                    <!--android:src="@drawable/logo_with_dropshadow" />-->

                <!--<TextView-->
                    <!--android:id="@+id/login_fragment_app_title"-->
                    <!--android:layout_width="wrap_content"-->
                    <!--android:layout_height="wrap_content"-->
                    <!--android:layout_margin="5dp"-->
                    <!--android:text="@string/digital_door_viewer"-->
                    <!--android:textAllCaps="true"-->
                    <!--android:textSize="18sp" />-->

            <!--</LinearLayout>-->

            <LinearLayout
                android:orientation="vertical"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <EditText
                    android:id="@+id/login_fragment_email_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="7dp"
                    android:background="@color/white"
                    android:hint="@string/email"
                    android:inputType="textEmailAddress"
                    android:padding="8dp"
                    android:singleLine="true"
                    android:textSize="20sp"/>

                <EditText
                    android:id="@+id/login_fragment_password_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white"
                    android:hint="@string/password"
                    android:inputType="textPassword"
                    android:padding="8dp"
                    android:singleLine="true"
                    android:textSize="20sp" />

                <Button
                    android:id="@+id/login_fragment_register_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginBottom="7dp"
                    android:layout_marginTop="10dp"
                    android:background="@android:color/transparent"
                    android:minHeight="0dp"
                    android:text="@string/register"
                    android:textAllCaps="false"
                    android:textSize="16sp" />

                <Button
                    android:id="@+id/login_fragment_forgot_password"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginBottom="15dp"
                    android:layout_marginTop="5dp"
                    android:background="@android:color/transparent"
                    android:minHeight="0dp"
                    android:text="@string/forgot_your_password"
                    android:textAllCaps="false"
                    android:textSize="16sp" />

                <Button
                    android:id="@+id/login_button"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:background="@drawable/yellow_selector"
                    android:minHeight="0dp"
                    android:paddingBottom="7dp"
                    android:paddingTop="7dp"
                    android:text="@string/login"
                    android:textAllCaps="true"
                    android:textSize="18sp" />

            </LinearLayout>

        </LinearLayout>

    </ScrollView>
</RelativeLayout>

出了什么问题,当我取消注释屏幕的标题时,它不会向上滚动,但注释掉它会按预期工作......我错过了什么?

【问题讨论】:

  • 我看到了你的网站。你更圆了。哈在所有领域。
  • :) 尽量做到至少
  • 坚持下去....我怎样才能下载你的歌曲???
  • 给我发一个github issue,我会折腾下载github.com/isuPatches/RockThePatch
  • 您可以发布您的编辑文本的代码吗?

标签: android android-layout scrollview


【解决方案1】:

尝试滚动到视图:

ScrollView scrollView = (ScrollView)findViewById(R.id.scrollview);
EditText editText = (EditText)findViewById(R.id.editText);
int[] coordinates = new int[2];
editText.getLocationOnScreen(coordinates);
scrollView.smoothScrollTo(coordinates[0],coordinates[1]);

【讨论】:

  • 我已经尝试了几次......我发现这条路线的问题是它在第一次弹出键盘时不会滚动
  • @isuPatches - 你在使用全屏模式吗?
  • 所以我的应用程序主题继承自&lt;style name="AppThemes" parent="Theme.AppCompat.Light.NoActionBar"&gt;,我尝试使用标志,但仍然没有得到所需的行为&lt;item name="android:windowFullscreen"&gt;false&lt;/item&gt; 我认为这与 scollview 当时不可滚动有关键盘出现是因为后续调用有效。我正在视图的 OnClick 中进行滚动调用,因为我想不出更好的放置它们的位置
  • 所以我尝试了一些东西,并且起作用的是添加 1700-2000ms 的 postDelayed 然后滚动。问题似乎是因为键盘正在打开滚动不起作用但是当它打开时它很好。我仍在努力寻找一种动态等待这种情况发生的方法。
猜你喜欢
  • 1970-01-01
  • 2012-06-29
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 2020-08-15
  • 2014-08-20
相关资源
最近更新 更多