Android - 按住按钮时禁用滚动

时间:2017-03-08 06:01:45

标签: android android-button ontouchlistener android-scrollview motionevent

我在Button内有一个ScrollView

<ScrollView>
    <LinearLayout>
        ...
        <Button/>
        ...
    <LinearLayout/>
</ScrollView>

我正在使用此按钮重新录制语音。当它捕获MotionEvent.ACTION_DOWN事件时,我开始重新编码,并且在MotionEvent.ACTION_DOWN事件中我停止它。

buttomRecord.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            startRecording();
            return true;
        }
        else if(event.getAction() == MotionEvent.ACTION_UP){
            stopRecording();
            return true;
        }
        return false;
    }
});

问题是当用户录制语音时,如果此ScrollView向下或向上滚动,此按钮将失去焦点并且永远不会捕获MotionEvent.ACTION_UP事件。

如何在我按住Button或禁用ScrollView滚动时将焦点锁定在 public class Employee { [Key] [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid EmployeeId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public DateTime JoiningDate { get; set; } public string Username { get; set; } public string Password { get; set; } } 上?

2 个答案:

答案 0 :(得分:3)

您可以使用

mScrollView.requestDisallowInterceptTouchEvent(true);

禁止scrollview的触碰事件

所以你的代码将是

    buttomRecord.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            startRecording();
            mScrollView.requestDisallowInterceptTouchEvent(true);
            return true;
        }
        else if(event.getAction() == MotionEvent.ACTION_UP){
            stopRecording();
            mScrollView.requestDisallowInterceptTouchEvent(false);
            return true;
        }
        return false;
    }
});

答案 1 :(得分:0)

使用此自定义滚动视图:

public class CustomScrollView extends ScrollView {

private boolean mScrollable = true;

public void setScrollingEnabled(boolean enabled) {
    mScrollable = enabled;
}

public boolean isScrollable() {
    return mScrollable;
}

public CustomScrollView(Context context) {
    super(context);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // if we can scroll pass the event to the superclass
            if (mScrollable) return super.onTouchEvent(ev);
            // only continue to handle the touch event if scrolling enabled
            return mScrollable; // mScrollable is always false at this point
        default:
            return super.onTouchEvent(ev);
    }
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // Don't do anything with intercepted touch events if
    // we are not scrollable
    if (!mScrollable) return false;
    else return super.onInterceptTouchEvent(ev);
}}

然后在你的xml中:

<your.package.name.CustomScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            Android: scrollbars="vertical">
   <!--whatever widgets you want to add-->
</your.package.name.CustomScrollView>

在您的活动中:

buttomRecord.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            scrollView.setScrollingEnabled(false);
            startRecording();
            return true;
        }
        else if(event.getAction() == MotionEvent.ACTION_UP){
            stopRecording();
            return true;
        }
        return false;
    }
});