Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

android - Activity's onTouchEvent() is not being called while clicking on Button

I want to do some B.logic in Activity.onTouchEvent() method. It is working fine for TextView and other non clickable views. However, it is not getting called uponclicking of Button. But I need that too.

Here is the code.

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Oh shit!! click performed... :(", Toast.LENGTH_LONG).show();

            }
        });



    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG, "ssssssssssssssssssssssss:touch");
        return super.onTouchEvent(event);
    }

    public boolean dispatchTouchEvent(MotionEvent event) {
        int eventaction=event.getAction();

        switch(eventaction) {
        case MotionEvent.ACTION_MOVE:
            break;
        default:
            break;
        }

        return super.dispatchTouchEvent(event);
    }

}

Please help me to sort out from this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have used dispatchTouchEvent(MotionEvent ev) as required. Here is the solution.

private boolean isInSideClicked = false;
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View content = findViewById(R.id.textView1);
        int[] contentLocation = new int[2];
        content.getLocationOnScreen(contentLocation);
        Rect rect = new Rect(contentLocation[0], 
                             contentLocation[1], 
                             contentLocation[0] + content.getWidth(), 
                             contentLocation[1] + content.getHeight());
        
        View frame = findViewById(R.id.editText1);
        int[] frameLocation = new int[2];
        frame.getLocationOnScreen(frameLocation);
        Rect framerect = new Rect(frameLocation[0], 
                                  frameLocation[1], 
                                  frameLocation[0] + frame.getWidth(), 
                                  frameLocation[1] + frame.getHeight());
        Log.d(TAG, "rect:  "+rect.bottom+" , "+rect.top+" , "+rect.left+" , "+rect.right);
        Log.d(TAG, "FrameRect:  "+framerect.bottom+" , "+framerect.top+" , "+framerect.left+" , "+framerect.right);
        Log.d(TAG, "x: "+event.getX()+"  y: "+event.getY());
        
        if ((rect.contains((int)event.getX(), (int)event.getY()) || framerect.contains((int)event.getX(), (int)event.getY()) ) ) {
            isInSideClicked = true;
        }
        if (isInSideClicked){
            return super.dispatchTouchEvent(event); 
        } else {
            return true;
        }
    } else if (event.getAction() == MotionEvent.ACTION_UP && isInSideClicked) {
        isInSideClicked = false;
        return super.dispatchTouchEvent(event);
    } else if (event.getAction() == MotionEvent.ACTION_MOVE && isInSideClicked) {
        return super.dispatchTouchEvent(event);
    } else {
        isInSideClicked = false;
        return true; 
    }
}

Below is the hierarchy on how the touch event is processed by OS level.

Activity.dispatchTouchEvent()
ViewGroup.dispatchTouchEvent()
View.dispatchTouchEvent()
View.onTouchEvent()
ViewGroup.onTouchEvent()
Activity.onTouchEvent()

With this I have restricted the touch events of the views which are not in required layouts/views.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...