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

Phylicia ·
更新时间:2024-09-20
· 753 次阅读

在网上搜了一下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); } } 您可能感兴趣的文章:android TextView不用ScrollViewe也可以滚动的方法完美解决EditText和ScrollView的滚动冲突(下)Android中实现监听ScrollView滑动事件Android开发控制ScrollView滑动速度的方法Android中ScrollView实现滑动距离监听器的方法Android ScrollView取消惯性滚动的方法



edittext scrollview

需要 登录 后方可回复, 如果你还没有账号请 注册新账号