ScrollView与EditText滑动冲突解决办法

在ScrollView中有EditText,当EditText部分内容被隐藏的时候,需要滑动EditText,让隐藏的内容显示出来,但是这个时候EditText滑动事件会和ScrollView的滑动事件冲突。为了解决这个问题可以自定义EditText
1.自定义EditText

public class ScrollviewEditText : EditText 
    {

        private ScrollView parentScrollView;

        public ScrollView GetParentScrollView() {  

            return parentScrollView;  

        }
        public void SetParentScrollView(ScrollView parent_scrollview) {  

            this.parentScrollView = parent_scrollview;  

        } 
        public ScrollviewEditText(Context context, IAttributeSet attrs, int defStyle) :base(context,attrs,defStyle){

        }

        public ScrollviewEditText(Context context, IAttributeSet attrs):base(context,attrs) {
        }

        public ScrollviewEditText(Context context) :base(context){

        }
        public override bool OnTouchEvent (MotionEvent e)
        {
            if (parentScrollView == null) {  
                return base.OnTouchEvent(e);
            } else {  
                if (e.Action == MotionEventActions.Down) {  
                    // 将父scrollview的滚动事件拦截  
                    SetParentScrollAble(false);  
                    //将父scrollview的滚动事件拦截   
                    return base.OnTouchEvent(e);
                } else if (e.Action == MotionEventActions.Up) {  
                    // 把滚动事件恢复给父Scrollview  
                    SetParentScrollAble(true);  
                    //把滚动事件恢复给父Scrollview----- 
                } else if (e.Action == MotionEventActions.Move) { 

                }  
            }  
            return base.OnTouchEvent (e);
        }
        //是否把滚动事件交给父scrollview 
        private void SetParentScrollAble(bool flag) {   
            parentScrollView.RequestDisallowInterceptTouchEvent (!flag);
        } 
    }

2.主界面关键代码

private ScrollviewEditText et_content;
private ScrollView scrollview;
et_content = FindViewById<ScrollviewEditText> (Resource.Id.et_content);
scrollview = FindViewById<ScrollView> (Resource.Id.scrollview);
et_content.SetParentScrollView (scrollview);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值