完美解决EditText和ScrollView的滚动冲突(上)

完美解决EditText和ScrollView的滚动冲突(上),第1张

概述在网上搜了一下EditText和ScrollView的滚动冲突,发现几乎所有的解决方案都是触摸EditText的时候就将事件交由EditText处理,否则才将事件交由ScrollView处理。这样确实初步解决了两者之间的滚动冲突,但并不是最好的

在网上搜了一下EditText和ScrollVIEw的滚动冲突,发现几乎所有的解决方案都是触摸EditText的时候就将事件交由EditText处理,否则才将事件交由ScrollVIEw处理。这样确实初步解决了两者之间的滚动冲突,但并不是最好的解决方案。比如,EditText本来可以显示6行文本,但是目前只显示了5行文本,此时我们在EditText区域进行滑动并期望整个页面能够滚动,但由于我们将事件交给了EditText进行处理,所以页面并不能滚动,这样的体验是极差的。其实我们更希望当EditText出现滚动条的时才将滚动事件交由它本身处理,其他情况下应当让ScrollVIEw来处理。那么该如何进行实现呢?接下来咱们就做一个小Demo来实现这种方案。

1.布局文件

首先编写布局文件,可以看出这是非常简单的一个布局:一个ScrollVIEw包裹着一个垂直方向的linearLayout,linearLayout中有两个TextVIEw和一个EditText,其中为了区分EditText的范围,给其设置了一个背景rectangle_shape。

<ScrollVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical"> <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="300dp" androID:text="Hello World Begin!"/> <EditText androID:ID="@+ID/edit_text" androID:hint="EditText" androID:layout_wIDth="match_parent" androID:layout_height="200dp" androID:gravity="top" androID:background="@drawable/rectangle_shape"/> <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="300dp" androID:text="Hello World End!"/> </linearLayout></ScrollVIEw>

2.rectangle_shape

背景rectangle_shape的代码,更没有什么技术含量。。。。。。

<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID"> <solID androID:color="#ffffff"/> <stroke androID:color="#cccccc" androID:wIDth="1dp"/></shape>

3.MainActivity中的代码

这里就是主要的代码逻辑了。先给EditText设置OntouchListener,然后先在Ontouch方法中判断当前点击的区域是否为EditText,如果为EditText区域则再判断是否可以在垂直方向上进行滚动,如果可以滚动则将事件交由EditText处理,否则将事件交由ScrollVIEw处理。
此处最重要的就是如何判断EditText区域在垂直方向上可以滚动,此处的代码已经封装成了一个方法,大家可以直接使用。那么为什么要这样判断呢?如果大家仍有兴趣,请继续阅读完美解决EditText和ScrollVIEw的滚动冲突(下)。

public class MainActivity extends Activity implements VIEw.OntouchListener {  private EditText mEditText;  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    mEditText = (EditText) findVIEwByID(R.ID.edit_text);    mEditText.setontouchListener(this);  }  @OverrIDe  public boolean ontouch(VIEw vIEw,MotionEvent motionEvent) {    //触摸的是EditText并且当前EditText可以滚动则将事件交给EditText处理;否则将事件交由其父类处理    if ((vIEw.getID() == R.ID.edit_text && canVerticalScroll(mEditText))) {      vIEw.getParent().requestdisallowIntercepttouchEvent(true);      if (motionEvent.getAction() == MotionEvent.ACTION_UP) {        vIEw.getParent().requestdisallowIntercepttouchEvent(false);      }    }    return false;  }  /**   * EditText竖直方向是否可以滚动   * @param editText 需要判断的EditText   * @return true:可以滚动  false:不可以滚动   */  private boolean canVerticalScroll(EditText editText) {    //滚动的距离    int scrollY = editText.getScrollY();    //控件内容的总高度    int scrollRange = editText.getLayout().getHeight();    //控件实际显示的高度    int scrollExtent = editText.getHeight() - editText.getCompoundpaddingtop() -editText.getCompoundpaddingBottom();    //控件内容总高度与实际显示高度的差值    int scrollDifference = scrollRange - scrollExtent;    if(scrollDifference == 0) {      return false;    }    return (scrollY > 0) || (scrollY < scrollDifference - 1);  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的完美解决EditText和ScrollView的滚动冲突(上)全部内容,希望文章能够帮你解决完美解决EditText和ScrollView的滚动冲突(上)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1149143.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存