EditText和ScrollView的滚动冲突

参考:
完美解决EditText和ScrollView的滚动冲突(上)
完美解决EditText和ScrollView的滚动冲突(下)

EditText输入框的内容超过高度或最大行数就可垂直滚动

定义最大行数:

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rectangle_shape"
            android:gravity="top"
            android:hint="输入框"
            android:maxLines="3"
            android:minLines="3" />

定义固定高度:

    <EditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/rectangle_shape"
            android:gravity="top"
            android:hint="输入框" />

当EditText处于ScrollView中时,
1、内容超过高度或最大行数EditText不能滚动,部分内容看不到。
2、内容没超过高度或最大行数EditText时,要随ScrollView一起滚动。

解决示例:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    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:gravity="center"
            android:text="Hello World Begin!" />


        <EditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/rectangle_shape"
            android:gravity="top"
            android:hint="输入框" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:gravity="center"
            android:text="Hello World End!" />
    </LinearLayout>

</ScrollView>


/**
 * 监听软键盘,在软键盘关闭时显示其他
 */
public class MainActivityI extends AppCompatActivity implements View.OnTouchListener {

    private EditText mEditText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maini);

        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);//告诉父view,我的事件自己处理
            if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                view.getParent().requestDisallowInterceptTouchEvent(false);//告诉父view,你可以处理了
            }
        }
        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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值