Adnroid的软键盘

关于Adnroid的软键盘

今天在做聊天输入框的时候,发现弹出的软键盘把获得焦点的EditText控件给挡住了,于是找了相关的API。官方提供了一系列的配置参数可供选择。在清单文件中对应的Activity中的windowSoftInputMode属性选择

  • SOFT_INPUT_ADJUST_NOTHING

  • SOFT_INPUT_ADJUST_PAN

  • SOFT_INPUT_ADJUST_RESIZE

  • SOFT_INPUT_ADJUST_UNSPECIFIED

      <activity
          android:windowSoftInputMode="stateHidden|adjustNothing"
          android:name=".MainActivity"
          android:label="@string/app_name">
          <intent-filter>
              <action android:name="android.intent.action.MAIN"/>
              <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>
      </activity>
    

如上配置即可。

  • adjustNothing不调整;
  • adjustPan会将整个页面都顶上去,为了把输入框显示出来;
  • adjustResize会重新调整压缩页面的尺寸,把软键盘需要的空间让出来,看起来就像被压扁了;
  • adjustUnspecified作为一种默认的配置,系统自己根据内容自行选择上两种方式的一种执行。

主要就是adjustPanadjustResize之间选择了,可是两种都达不到预期的效果,达不到当软键盘弹出时背景不变形,位置不变化,只是EditText的位置始终在软键盘的顶部,弹出的时候把背景给盖住了。看起来就像是两部分,输入框在动,可以把背景给盖住。


最后,还是把布局分为了两部分,背景部分用一个ScrollView包裹了起来,这样就不会因为软键盘的弹出,一会儿被压扁或者被顶上去了。顶上去的只有EditText这个输入框,windowSoftInputMode模式还是选择adjustResize这个,用adjustPan的话,背景还是会被顶上去,不管是不是加了ScrollView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    >

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

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/a"/>

    </LinearLayout>
</ScrollView>

<EditText
    android:id="@+id/mEt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

这样就达到预期的效果了

还有美女^^

1.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容