Android:底部对话框中的多行文本EditText

13

我有一个底部对话框,并且在布局中存在EditText。EditText是多行的,最大行数为3。我放置了:

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

但当用户开始垂直滚动EditText文本时,BottomSheetBehavior会拦截事件,使EditText无法垂直滚动。

enter image description here

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


你是如何使用AlertDialog或PopupWindow创建这个对话框的? - Jai
不,这只是我自定义的带有BottomSheetBehavior的布局。 - SBotirov
在这种情况下,您必须使用onDispatchTouchEvent将触摸事件传递给前面的对话框表,并且您可以解决此问题!您遇到此类问题是因为您的BottomSheet存在某些问题,导致其获得了焦点。 - Jai
如果您不想使用触摸事件,您可以在AlertDialog中打开它,或者PopupWindow可能是更好的选择。 - Jai
3个回答

20

这里有一个简单的方法来完成它。

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

7

对于那些对 Kotlin 解决方案感兴趣的人,这是它

editText.setOnTouchListener { v, event ->
    v.parent.requestDisallowInterceptTouchEvent(true)
    when (event.action and MotionEvent.ACTION_MASK) {
        MotionEvent.ACTION_UP -> 
                      v.parent.requestDisallowInterceptTouchEvent(false)
    }
    false
}

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

  1. I created custom work around bottom sheet behavior extends native android BottomSheetBehavior:

    public 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. then set touch event of EditText and when user touching area of EditText I will be disable handling event by parent with calling method 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;
    }
    });
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接