开发过程中会有这样的需求:
当在某一个文本输入框中输入文本时,需要在文本框底部提示“还可输入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>
效果如图: