在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);