温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

详解Android ScrollView嵌套EditText出现的滑动问题

发布时间:2020-08-25 20:03:35 来源:脚本之家 阅读:202 作者:黑石ZB 栏目:移动开发

今天项目中需求是写出一个很简单的edittext输入框,但要求当输入字数过长时需要上下滑动以便查看所有文字,因为页面底部有一个"确定"的button,但刚开始输入框内的问题怎么都滑动不了,我一开始就想到了这是事件传递冲突问题,但试了很多种方法都不行,最后也是一个一个试才解决的,不多说,贴代码:

<ScrollView
    android:id="@+id/sc_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:minHeight="360dp"
    android:scrollbars="none">
    <EditText
      android:id="@+id/editText"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginEnd="10dp"
      android:layout_marginStart="15dp"
      android:layout_marginTop="10dp"
      android:background="@null"
      android:gravity="top|start"
      android:hint="@string/FeedBackViewController_Placeholder"
      android:lineSpacingMultiplier="1.0"
      android:paddingEnd="10dp"
      android:paddingStart="10dp"
      android:maxHeight="450dp" //当初这个没加,也出现了滑动不了的情况
      android:textSize="@dimen/font_size16"/>
  </ScrollView>

代码里面需要:

editText.setOnTouchListener(new View.OnTouchListener() {
        @Override
          public boolean onTouch(View v, MotionEvent event) {
           // 解决scrollView中嵌套EditText导致不能上下滑动的问题
             v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
              case MotionEvent.ACTION_UP:
            v.getParent().requestDisallowInterceptTouchEvent(false);
            break;
           }
           return false;
          }
      });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI