Android onTouchListener for entire dialog

假装没事ソ 提交于 2019-11-29 11:59:53

You should create a class which extends the dialog class and then you can create a gesture detector and attach to the ontouchevent of the class. Here is a code example:

public class GestureDialog extends Dialog {
    public GestureDialog (Context context, int theme) {
        super(context, theme);

        gestDetec = new MyGestureDetector();    //inital setup
        gestureDetector = new GestureDetector(gestDetec);   
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        else
            return false;
    }
class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        System.out.println( "Help im being touched!" );
        return false;
    }
}

}

You can Take one activity and set it's theme in manifest to dialog.

Then implements onTouchListener to that activty and write one onTouch() event in that Activity.

Now you got Touch event for entire dialog :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!