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

515 查看

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

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

<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和屏幕高度来实现,几乎是一项无法完成的任务,还希望看到我这篇文章的大神能够给小弟指点。既然无法融合,只能另辟蹊径,所有我想到了另一种方法,下面我把实现方法简单梳理一下

实现方法

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

 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被软键盘挡住了,通过查阅网上的资料,总体来说,有四种解决方案

方法一
在你的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。这样就会把软键盘和输入框一起滚动了,软键盘会一直处于底部。

<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