Android:BottomSheetDialog中的多行文本EditText

问题描述 投票:5回答:2

我有一个底部对话框,并在布局中存在EditText。 EditText是多行,最大行是3.我把:

commentET.setMovementMethod(new ScrollingMovementMethod());
commentET.setScroller(new Scroller(bottomSheetBlock.getContext()));
commentET.setVerticalScrollBarEnabled(true);

但是当用户将开始垂直滚动EditText文本时,BottomSheetBehavior拦截事件和EditText将不会垂直滚动。

enter image description here

有谁知道如何解决这个问题?

android scroll android-edittext bottom-sheet
2个回答
8
投票

这是一个简单的方法。

yourEditTextInsideBottomSheet.setOnTouchListener(new OnTouchListener() {
  public boolean onTouch(View v, MotionEvent event) {
        v.getParent().requestDisallowInterceptTouchEvent(true);
        switch (event.getAction() & MotionEvent.ACTION_MASK){
        case MotionEvent.ACTION_UP:
            v.getParent().requestDisallowInterceptTouchEvent(false);
            break;
        }
        return false;
   }
});

1
投票

我用以下方法解决了这个问题:

  1. 我创建自定义工作围绕底部表单行为扩展本机android BottomSheetBehaviorpublic class WABottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> { private boolean mAllowUserDragging = true; public WABottomSheetBehavior() { super(); } public WABottomSheetBehavior(Context context, AttributeSet attrs) { super(context, attrs); } public void setAllowUserDragging(boolean allowUserDragging) { mAllowUserDragging = allowUserDragging; } @Override public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) { if (!mAllowUserDragging) { return false; } return super.onInterceptTouchEvent(parent, child, event); } }
  2. 然后设置EditText的触摸事件,当用户触摸EditText区域时,我将通过调用方法setAllowUserDragging禁用处理事件: commentET.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (v.getId() == R.id.commentET) { botSheetBehavior.setAllowUserDragging(false); return false; } return true; } });
© www.soinside.com 2019 - 2024. All rights reserved.