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

发布时间:2019-06-23 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了如何实现EditText随ScrollView高度变化,到达屏幕底部固定脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

序言
设置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

脚本宝典总结

以上是脚本宝典为你收集整理的如何实现EditText随ScrollView高度变化,到达屏幕底部固定全部内容,希望文章能够帮你解决如何实现EditText随ScrollView高度变化,到达屏幕底部固定所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: