android - 软键盘隐藏编辑文本

标签 android android-edittext android-softkeyboard window-soft-input-mode

我的布局底部有一个 EdiText。我遇到的问题是,当它获得焦点时,屏幕会向上移动以给软键盘留出空间。为了解决这个问题,我添加了

android:windowSoftInputMode="adjustResize"

ma​​nifest 中我的 Activity 声明中。现在的问题是编辑文本隐藏在软键盘后面,尽管屏幕没有移动。 相反,如果我把

android:windowSoftInputMode="adjustPan"

然后 Activity 的主窗口向上移动,这是我不希望的。 我该如何解决这个问题,当 EdiText 获得焦点时, Activity 的主窗口不会向上移动以为软键盘腾出空间,而是调整主窗口的大小,包括 EdiText因此键盘位于 EdiText 下方,如在许多应用中所见。

布局

<?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:background="@color/background_feed">


    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal|center_vertical" />



    <ListView
        android:id="@+id/comments"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F7F7F7"
        android:paddingTop="10dp"
        android:layout_above="@+id/border_view_comment"
        android:divider="@android:color/transparent"
        android:dividerHeight="10dp"
        android:paddingBottom="10dp"
        android:clipToPadding="false"
        android:scrollbarStyle="outsideOverlay" />

    <TextView
        android:id="@+id/empty_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/no_comment_msg"
        android:visibility="gone"/>

    <View
        android:id="@+id/border_view_comment"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/commentlayout"
        android:background="#C7C7C7"
        android:visibility="gone"/>


    <RelativeLayout
        android:id="@+id/commentlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/comment_box_shadow"
        android:padding="10dp">

        <ImageView
            android:id="@+id/commentor_image"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginRight="15dp"
            />

        <ImageView
            android:id="@+id/clickPostComment"
            android:layout_width="30dp"
            android:layout_height="45dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/post"
            android:textColor="@color/white" />

        <EditText
            android:id="@+id/commentsPost"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_alignTop="@id/commentor_image"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@id/clickPostComment"
            android:layout_toRightOf="@id/commentor_image"
            android:background="@drawable/comment_edittext_bg"
            android:hint="Comments"
            android:singleLine="true"
            android:maxLength="140"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:textColor="@color/title_gray"
            android:textColorHint="#d5d5d5"
            android:textSize="15sp" />

        <ImageView
            android:layout_width="25dp"
            android:layout_height="30dp"
            android:layout_alignTop="@id/commentsPost"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="-2dp"
            android:layout_toLeftOf="@id/commentsPost"
            android:scaleType="fitStart"
            android:src="@drawable/comment_shape" />
    </RelativeLayout>
</RelativeLayout>

最佳答案

通过将 View 嵌入到 Scrollview 中使 View 可滚动

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
   .....
    add your layout
   ......

</ScrollView>

如果这不能帮助您将布局设置为 RelativeLayout 并使用 RelativeLayout 设计您的布局

关于android - 软键盘隐藏编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31228219/

相关文章:

android - 使用 Android Paging 3 库时如何显示空 View

android - Eclipse IDE 超出 Java 堆空间 - Windows XP

Android 在 EditText 小部件上动态设置 9 补丁背景

android - 在软键盘上显示 'Done' 键

android - 软键盘阻止 EditText

php - 当从移动应用程序生成访问 token 时,如何验证来自 PHP 的访问 token ?

android - 编译时无法打开数据库文件(代码 14): ,:PRAGMA journal_mode

android - 安卓浏览器的操作栏

android - 如何将两个EditText放在同一行

c# - 单击外部 Edittext 后如何在 Xamarin Android 中隐藏键盘