美文网首页
Adnroid的软键盘

Adnroid的软键盘

作者: Sxgg | 来源:发表于2015-09-20 22:25 被阅读0次

    关于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

    相关文章

      网友评论

          本文标题:Adnroid的软键盘

          本文链接:https://www.haomeiwen.com/subject/vrqicttx.html