如何实现EditText随ScrollView高度变化,到达屏幕底部固定

序言
设置EditText位于ScrollView下方,当ScrollView的高度大于屏幕高度,EditText就会被挤出屏幕,而若是把EditText固定在屏幕底部,当ScrollView的高度小于屏幕高度,ScrollView和EditText之间就会出现一段空白,那么如何实现EditText随ScrollView高度变化,到达屏幕底部固定?android

分析
要实现EditText随ScrollView高度变化,布局通常是这样的:ide

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"  
     
  <ScrollView
     android:id="@+id/dvp_scroll_view"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="#D6DADD"
     android:overScrollMode="never"
     android:padding="2dp"
     android:scrollbars="none" />
    
  <EditText
    android:id="@+id/edittext1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ***android:layout_below="@id/dvp_scroll_view"***
    android:background="#EEEFF1" />

</RelativeLayout>

而要实现固定在屏幕底部,通常布局是这样的:布局

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"  
         
      <ScrollView
         android:id="@+id/dvp_scroll_view"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="#D6DADD"
         android:overScrollMode="never"
         android:padding="2dp"
         android:scrollbars="none" />
        
      <EditText
        android:id="@+id/edittext2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ***android:layout_alignParentBottom="true"***
        android:background="#EEEFF1" />
    
    </RelativeLayout>

怎么才能把他们融合到一块儿呢,据我目前掌握的知识和查阅的资料来看,要想经过设置某个属性,或者计算scrollview和屏幕高度来实现,几乎是一项没法完成的任务,还但愿看到我这篇文章的大神可以给小弟指点。既然没法融合,只能另辟蹊径,全部我想到了另外一种方法,下面我把实现方法简单梳理一下post

实现方法线程

在布局中放置两个EditText,一个设置为android:layout_below="@id/scrollview,称为“EditText1”另外一个设置为android:layout_alignParentBottom="true",称为“EditText2”布局很简单,这里就不贴代码了
首先隐藏EditText2,计算ScrollView的高度或者EditText1的坐标,若是大于屏幕高度,就隐藏EditText1,显示EditText2,计算方法有不少种,这里先贴一种我使用的方法,但无论用哪一种计算方法,都要注意调用view.post(new Runnable(){});把计算的代码放到线程里,否则计算的结果永远是0;code

EditText1.post(new Runnable() {  
                @Override
                public void run() {
                    int[] position = new int[2];
                    EditText1.getLocationInWindow(position);
                    WindowManager wm = (WindowManager)                 getSystemService(Context.WINDOW_SERVICE);
                    if (position[1] + EditText1.getHeight() > wm
                            .getDefaultDisplay().getHeight()) {
                        EditText1.setVisibility(View.GONE);
                        EditText2.setVisibility(View.VISIBLE);
                    }
                }   
            });

经过运行程序,确认这种方法是可行的,可是还有一个问题,当点击EditText弹出软键盘时,EditText2会自动上移,而EditText1被软键盘挡住了,经过查阅网上的资料,整体来讲,有四种解决方案xml

方法一
在你的activity中的oncreate中setContentView以前写上这个代码
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);接口

方法二
在 项目的AndroidManifest.xml文件中界面对应的<activity>里加入
<activity android:windowSoftInputMode="stateVisible|adjustResize" >
在这设置的值(除"stateUnspecified"和"adjustUnspecified"之外)将覆盖在主题中设置的值生命周期

方法三
把顶级的layout替换成ScrollView,或者说在顶级的Layout上面再加一层ScrollView。这样就会把软键盘和输入框一块儿滚动了,软键盘会一直处于底部。ci

<ScrollView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      
     <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"  
             
          <ScrollView
             android:id="@+id/dvp_scroll_view"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="#D6DADD"
             android:overScrollMode="never"
             android:padding="2dp"
             android:scrollbars="none" />
            
          <EditText
            android:id="@+id/edittext1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ***android:layout_below="@id/dvp_scroll_view"***
            android:background="#EEEFF1" />
        
        </RelativeLayout>
         
     </ScrollView>

方法四
自定义LinearLayoutView
该组件可实现根据软键盘的弹出/关闭而隐藏和显示某些区域,这是问题解决最关键部分,主要有两点:
① 重写onSizeChanged方法
该方法是View生命周期的方法,当View尺寸发生变化时调用,如竖屏横屏切换、软键盘弹出。这里当软键盘弹出形成View尺寸改变,就会调用onSizeChanged方法,在该方法实现代码的核心思想是根据尺寸变化,当变大(软键盘弹出),将某些区域隐藏以给编辑界面预留出足够显示空间;当恢复(软键盘关闭),再将隐藏的区域显示出来
②提供KeyBordStateListener 接口采用回调机制调用接口的实现方法。

经过验证,方法一和二都是把整个屏幕上移,不是我想要的效果,方法三简单可行,解决了EditText被软键盘遮挡的问题,到此问题基本获得解决

总结整个过程当中容易被忽视的两点:1.计算控件宽高度和坐标的代码须要放到线程中2.只有scrollview在软键盘弹出时,会自动压缩所占空间,因此要想不被软件盘挡住,就要把在最外层多套一层scrollview

相关文章
相关标签/搜索