Android键盘隐藏了EditText

时间:2014-06-17 11:19:54

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

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

<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之前和之后的图像。

screenshot without soft keyboard, EditText visible

screenshot with soft keyboard, EditText hidden

10 个答案:

答案 0 :(得分:50)

在你的清单中将这个放在你的特定活动中:

 android:windowSoftInputMode="adjustPan" 

答案 1 :(得分:15)

为此你必须在你的活动表现中声明

         <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>

答案 2 :(得分:4)

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

Option Explicit

Sub CreatePivot()

' Creates a PivotTable report from the table on Sheet1
' by using the PivotTableWizard method with the PivotFields
' method to specify the fields in the PivotTable.

Dim objTable As PivotTable
Dim objTblCache As PivotCache
Dim objField As PivotField
Dim PvtSht As Worksheet
Dim LastRow As Long, LastCol As Long
Dim SrcRng  As Range

' Select the sheet and first cell of the table that contains the data
With ThisWorkbook.Sheets("Sheet1")
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column

    Set SrcRng = .Range(.Cells(2, "A"), .Cells(LastRow, LastCol)) ' <-- set the Pivot's dynamic Range (starts from "A2")
End With

' Option 1: Define Pivot Cache
Set objTblCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, SrcRng)

' Option 2: Define Pivot Cache
Set objTblCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcRng.Address(True, True, xlA1, True))

' Create the PivotTable object based on the Employee data on Sheet1.
Set objTable = Sheet1.PivotTables.Add(PivotCache:=objTblCache, TableDestination:=Sheet1.Cells(1, LastCol + 2), TableName:="PivotTable1")

' Specify row and column fields.
With objTable
    .PivotFields("v1").Orientation = xlColumnField
    .PivotFields("Temperature").Orientation = xlRowField

    ' Specify a data field with its summary
    ' function and format.
    Set objField = .PivotFields("clkui")
    With objField
        .Orientation = xlDataField
        .Function = xlSum
        .NumberFormat = "$ #,##0"
    End With

    ' Specify a page field.
    .PivotFields("db").Orientation = xlPageField
End With

' rest of your code goes here ....


End Sub

此代码适用于我。
在活动代码中的清单文件内添加此属性:

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

此属性(android:windowSoftInputMode="adjustResize|stateHidden" )的更多此类值将作为推荐列表。您也可以与他们核实。

答案 3 :(得分:2)

使用以下代码:

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);

答案 4 :(得分:2)

在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 :(得分:1)

对我来说,解决方案就是将其添加到父布局中: 机器人:fitsSystemWindows =&#34;真&#34;

答案 6 :(得分:0)

adjustPan模式设置

时,这似乎是android中的一个错误

尝试使用此自定义EditText并解决此问题的解决方法

https://gist.github.com/Arjun-sna/768de4ca141798c59a8166a2e5d4a743

答案 7 :(得分:0)

我通过在内部带有EditText的视图周围添加ScrollView来解决此问题。

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

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

答案 8 :(得分:0)

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

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 :(得分:0)

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

<块引用>

android:windowSoftInputMode="stateAlwaysHidden|adjustResize"