【问题标题】:Android Keyboard hides EditTextAndroid 键盘隐藏 EditText
【发布时间】:2014-08-07 09:11:59
【问题描述】:

当我尝试在屏幕底部的 EditText 中写入内容时,软键盘会隐藏 EditText。我该如何解决这个问题?下面是我的xml代码。我在 Fragment 中使用它。

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >


        <LinearLayout
            android:id="@+id/linearLayoutTopDetails"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearLayoutTop"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="6dp"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/list_design1"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="80dp"
                    android:layout_weight="100"
                    android:orientation="horizontal" >

                    <ImageView
                        android:id="@+id/ImageViewProfImagePostDetail"
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_marginTop="2dp"
                        android:layout_weight="24.84"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="5dp" />

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="60"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/textViewNamePostDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Robin"
                            android:textSize="17sp" />

                        <TextView
                            android:id="@+id/textViewLocationPostDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="1dp"
                            android:drawableLeft="@drawable/location"
                            android:text="beijing, China" />

                        <TextView
                            android:id="@+id/textViewTimeAgoPostDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=" 18min ago" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="15"
                        android:orientation="horizontal" >

                        <TextView
                            android:id="@+id/textViewReportAbusePostDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:background="@drawable/flag"
                            android:paddingLeft="5dp" />

                        <TextView
                            android:id="@+id/textViewDeletePostDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_marginLeft="15dp"
                            android:background="@drawable/delete"
                            android:visibility="gone" />
                    </LinearLayout>
                </LinearLayout>

                <TextView
                    android:id="@+id/textViewDescriptionPostDetail"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="2dp"
                    android:singleLine="false"
                    android:text="Description or caption of the post"
                    android:textSize="17sp" />
            </LinearLayout>

            <ImageView
                android:id="@+id/feedPostedImagePostDetail"
                android:layout_width="fill_parent"
                android:layout_height="200dp"
                android:layout_marginTop="5dp"
                android:scaleType="fitCenter"
                android:visibility="gone" />

            <LinearLayout
                android:id="@+id/linearLayoutOptions"
                android:layout_width="fill_parent"
                android:layout_height="33dp"
                android:layout_marginTop="2dp"
                android:layout_weight="100"
                android:background="@drawable/bar"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/textViewShakePostDetail"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="25"
                    android:drawableLeft="@drawable/like"
                    android:gravity="center"
                    android:singleLine="true"
                    android:text="Shake"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/textViewCommentPostDetail"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="41"
                    android:drawableLeft="@drawable/comment"
                    android:gravity="center"
                    android:paddingLeft="20dp"
                    android:text="Comment" />

                <TextView
                    android:id="@+id/textViewSharePostDetail"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="33"
                    android:drawableLeft="@drawable/share"
                    android:gravity="center"
                    android:paddingLeft="20dp"
                    android:text="Spread" />
            </LinearLayout>
        </LinearLayout>

        <ListView
            android:id="@+id/listViewFeedsDetail1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearLayoutTopDetails"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="6dp" >
        </ListView>

        <LinearLayout
            android:id="@+id/linearLayoutPostcomment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/listViewFeedsDetail1"
            android:background="#FFFFFF"
            android:orientation="horizontal"
            android:weightSum="100" >

            <EditText
                android:id="@+id/editTextComment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="80" />

            <Button
                android:id="@+id/buttonPostComment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="20"
                android:background="#F0F8FF"
                android:text="Post" >
            </Button>
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

我已尝试通过代码添加更改来解决此问题,但没有成功。

下面是点击EditText前后的图片。

【问题讨论】:

  • 滚动视图不起作用。
  • 将此属性:android:windowSoftInputMode="adjustPan|adjustResize" 添加到 AndroidManifest.xml 中的 Activity 标记
  • @HareshChhelana 我认为应该使用 adjustPan 或 adjustResize ,而不是同时使用两者。它们是相互矛盾的
  • 设置adjustPan 模式时,这似乎是android 中的一个错误尝试此自定义EditText 并解决此问题gist.github.com/Arjun-sna/768de4ca141798c59a8166a2e5d4a743

标签: android android-layout android-edittext android-scrollview android-keypad


【解决方案1】:

在清单中,在特定的活动标签上添加这一行:

 android:windowSoftInputMode="adjustPan" 

【讨论】:

  • 我试过这个但没有成功。 android:windowSoftInputMode="adjustPan|adjustResize" 此代码仍在我的清单中。但仍在隐藏 editText。
  • @RobinRoyal JFTR,adjustPan|adjustResize 与单独的 adjustPan 不同。事实上,如果其他用户后来所说的是真的,那么你永远不应该混合两个 adjust* 状态。
【解决方案2】:

为此,您必须在活动清单中声明

         <activity
            android:name=".activityname"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan|adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

【讨论】:

  • 这是我的活动清单代码。 这仍然是隐藏editText。
  • 这是片段而不是活动。我应该在哪里做呢?我已经将此代码放置在放置片段的活动中。
  • @RobinRoyal 可能是我迟到了,但是(我对其他人有用)使用adjustPan 或adjustResize,而不是同时使用两者
  • 不要将两个“调整...”值设置在一起,如文档所述:设置必须是下表中列出的值之一,或一个“状态...”的组合值加上一个“调整...”值。在任一组中设置多个值(例如,多个“状态...”值)具有未定义的结果。
【解决方案3】:

这里是简单的解决方案,解决Android EditText 隐藏在软键盘后面的问题。 使用项目模块的AndroidManifest.xml文件中的代码。

<activity
        android:name="com.example.MainActivity"
        android:label="@string/activity_main"
        android:windowSoftInputMode="adjustResize|stateHidden" />

这段代码对我有用。
在活动标签的清单文件中添加这个属性:

android:windowSoftInputMode="adjustResize|stateHidden"

此属性 (android:windowSoftInputMode) 有更多此类值,它们将作为推荐列表出现。您也可以与他们核实。

【讨论】:

    【解决方案4】:

    在 manifest.xml 文件中声明 windowSoftInputMode="adjustResize"

    <activity
            android:name=".example"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustResize" />
    

    为 xml 布局文件提供滚动视图

    <RelativeLayout
             xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:paddingBottom="16dp"
             android:paddingLeft="16dp"
             android:paddingRight="16dp"
             android:paddingTop="16dp"
             android:fitsSystemWindows="true">
    
    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:isScrollContainer="false">
    
       // add edittext here...
    
     </ScrollView>
     </RelativeLayout>
    

    【讨论】:

      【解决方案5】:

      使用以下代码:

      InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                      ipmm.hideSoftInputFromWindow(url.getWindowToken(), 0);
      

      我的代码中的 url 是:

      url = (EditText) findViewById(R.id.eT_webbrowser);
      

      或试试这个:

      InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                          ipmm.hideSoftInputFromWindow(null, 0);
      

      作为另一个选项试试这个:这总是隐藏软输入模式,这样你的 EditText 是可见的

      this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
      

      【讨论】:

      • @Robin Royal - 如果它适合你,请接受它作为答案。
      • 我没听懂你。但是,您是否也尝试使用“null”作为检查选项?
      • 是的,我试过了。当我点击 EditText 写东西时。软键盘显示但它隐藏了 EditText。实际上 EditText 没有在软键盘上向上滚动。EditText 停留在底部屏幕。
      • 试试这个。在我的答案中也进行了编辑: this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_AL‌​WAYS_HIDDEN); – 罗摩基什纳巴拉
      • 仍然没有发生任何事情。谢谢
      【解决方案6】:

      我通过在 View 周围添加带有 EditText 的 ScrollView 来处理这个问题。

      <ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              >
              // Other views
              <EditText ... />
      </ScrollView>
      

      向 Android Manifest 添加标签对我没有帮助。

      【讨论】:

        【解决方案7】:

        对我来说,解决方案是简单地将其添加到父布局中: android:fitsSystemWindows="true"

        【讨论】:

          【解决方案8】:

          将此行放在您的应用程序标签中:-

          android:windowSoftInputMode="stateHidden|adjustPan"

          例子:-

          <application
              android:name=".application.MyApplication"
              android:allowBackup="false"
              android:icon="@mipmap/ic_launcher"
              android:label="@string/app_name"
              android:largeHeap="true"
              android:networkSecurityConfig="@xml/network_security_config"
              android:roundIcon="@mipmap/ic_launcher_round"
              android:supportsRtl="false"
              android:theme="@style/AppTheme"
              android:windowSoftInputMode="stateHidden|adjustPan"
              tools:ignore="GoogleAppIndexingWarning"
              tools:replace="android:supportsRtl,android:allowBackup"
              tools:targetApi="n">
          

          【讨论】:

            【解决方案9】:

            在您的活动清单文件中添加这一行,看看它的神奇之处。

            android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

            【讨论】:

              【解决方案10】:

              试试这个,可能对你有帮助。

              将以下行添加到您的 EditText

              android:paddingVertical="16dp"
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-02-18
                • 2019-12-14
                • 2013-08-20
                • 1970-01-01
                相关资源
                最近更新 更多