【问题标题】:android - EditText height resize based on keyboardandroid - 基于键盘的 EditText 高度调整大小
【发布时间】:2013-12-05 10:13:12
【问题描述】:

我在 android 中有一个活动,其中有一个编辑框和编辑框下方的 3 个按钮。请查看附件。启动此活动时,默认状态为 STATE1。(请参见图片)。默认情况下键盘是可见的。现在,当我按下后退按钮或释放键盘时,我希望调整编辑文本的大小并占据整个屏幕,如 STATE2 所示。

我不知道如何做到这一点。我将edittext的高度硬编码为基于目标设备的某个dp。我相信这必须改变。任何人都可以帮助我如何做到这一点。

布局文件的 XML 和屏幕截图如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"        
    android:orientation="vertical" >

    <EditText
        android:id="@+id/EditMessage"
        android:layout_width="fill_parent"
        android:layout_height="150dp"            
        android:background="@drawable/newback"
        android:gravity="top"
        android:imeOptions="actionDone"
        android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions"
        android:maxLength="200"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
       />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"           
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/PostMessage"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:layout_marginRight="0.2dp"
            android:background="@drawable/newbutton_corner"
            android:text="@string/SubmitMessage"
            android:textColor="#FFFFFF"
            android:textStyle="bold" />

        <Button
            android:id="@+id/CancelMessage"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:layout_marginRight="0.2dp"
            android:background="@drawable/newbutton_corner"
            android:text="@string/CancelMessage"
            android:textColor="#FFFFFF"
            android:textStyle="bold" />

        <Button
            android:id="@+id/DeleteMessage"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:background="@drawable/newbutton_corner"
            android:text="@string/DeleteMessage"
            android:textColor="#FFFFFF"
            android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>  

【问题讨论】:

    标签: android android-layout android-activity android-edittext


    【解决方案1】:

    如下更改您的EditText

    <EditText
        android:id="@+id/EditMessage"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:imeOptions="actionDone"
        android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions"
        android:maxLength="200"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        />
    

    添加layout_weight = 1

    在我看来,您不需要需要adjustPan。我会让你决定。行为会有所不同,您可以尝试一下。以下是文档中关于 adjustPan 的内容:

    活动的主窗口未调整大小以为软体腾出空间 键盘。而是自动平移窗口的内容 这样当前的焦点就不会被键盘和用户遮挡 总能看到他们在输入什么。这通常不太理想 而不是调整大小,因为用户可能需要关闭软键盘才能 到达窗口的模糊部分并与之交互。

    如果使用此功能,您底部的按钮可能会被隐藏。 改为使用adjustResize: 把这一行放到activity里面AndroidManifest

    android:windowSoftInputMode="stateVisible|adjustResize"
    

    【讨论】:

    • 谢谢。我用了你的代码。我无法查看这三个按钮。键盘隐藏了按钮。在状态 1 中,显示了按钮。有什么指点吗??
    • 你需要在活动中添加这个(在AndroidManifest.xml内):android:windowSoftInputMode="stateVisible|adjustResize"查看更新的答案
    • 完美 10 为您提供帮助 Amulya Khare。非常感谢。非常高兴
    【解决方案2】:

    在你的清单中试试这个。

    <activity
          android:name="Activity"
          android:windowSoftInputMode="adjustPan"/>
    

    你的edittext使用&lt;requestFocus /&gt;,因为从那开始它自己就会专注

    android:layout_weight="1" 会自动填满屏幕。

     <EditText
            android:id="@+id/EditMessage"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"            
            android:background="#696969"
            android:gravity="top"
            android:imeOptions="actionDone"
            android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions"
            android:maxLength="200"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000"
           >
    
            <requestFocus />
    
        </EditText>
    

    【讨论】:

      【解决方案3】:

      是的,你可以这样做,很简单。

      1. android:windowSoftInputMode="adjustResize" ,, 将此属性添加到您需要的活动下的清单
      2. 将文本视图的宽度和高度设置为 match_parent

      完成。

      【讨论】:

        【解决方案4】:

        添加android:windowSoftInputMode="adjustPan"并将android:layout_height="150dp"更改为android:layout_height="fill_parent"

        【讨论】:

          猜你喜欢
          • 2015-11-12
          • 1970-01-01
          • 1970-01-01
          • 2011-10-09
          • 2013-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-24
          相关资源
          最近更新 更多