android – 基于键盘调整EditText高度

前端之家收集整理的这篇文章主要介绍了android – 基于键盘调整EditText高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 android中有一个活动,其中有一个编辑框和编辑框下面的3个按钮.请查看附件.启动此活动时,默认状态为STATE1.(请参阅图像).键盘默认可见.现在,当我按下后退按钮或处理键盘时,我希望重新调整edittext并占用整个屏幕,如STATE2所示.

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

布局文件XML和screencap一样

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

解决方法

按如下方式更改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的文档说明:

The activity’s main window is not resized to make room for the soft
keyboard. Rather,the contents of the window are automatically panned
so that the current focus is never obscured by the keyboard and users
can always see what they are typing. This is generally less desirable
than resizing,because the user may need to close the soft keyboard to
get at and interact with obscured parts of the window.

如果使用此按钮,底部的按钮可能会隐藏.而是使用adjustResize:
将此行放在AndroidManifest中的活动中

android:windowSoftInputMode="stateVisible|adjustResize"

猜你在找的Android相关文章