Android弹出软键盘时如何将底部顶上去

开发过程中会有这样的需求:

当在某一个文本输入框中输入文本时,需要在文本框底部提示“还可输入XX字”。此时,如果文本框是全屏的,或者高度太高,在软键盘弹出时底部的提示则会被键盘遮挡住,用户体验不是很好。

网上很流行的一种方式是设置android:windowSoftInputMode="stateUnchanged|adjustPan" ,但是在我的代码里面并不好用,而且这种方法影响的是整个Activity,感觉不太好。


解决方案:在EditText外面使用ScrollView做父布局。

代码如下:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/vLine" >

        <EditText
            android:id="@+id/edEvaluation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:gravity="top"
            android:hint="请您对自己做一段简短的评价,简明扼要的说明你最大的优势是什么,避免使用空洞老套的话。"
            android:imeOptions="flagNoEnterAction"
            android:inputType="textMultiLine"
            android:maxLength="1000"
            android:padding="@dimen/size_10_dp"
            android:scrollHorizontally="false"
            android:singleLine="false"
            android:textColorHint="@color/light_gray"
            android:textSize="@dimen/text_size_16" />
    </ScrollView>

    <View
        android:id="@+id/vLine"
        android:layout_width="match_parent"
        android:layout_height="@dimen/line_height"
        android:layout_above="@+id/tvWordNum"
        android:layout_marginLeft="@dimen/size_10_dp"
        android:background="@color/line_color" />

    <TextView
        android:id="@+id/tvWordNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:padding="@dimen/size_10_dp"
        android:text="@string/text_number" />

</RelativeLayout>


效果如图:



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值