开发者

Handling touch events in order

I want to write a program that handles touch events in a specific order within a single activity.

For example: A few views are shown. If the user doesn't tap on the first view, I will show another activity. If the user taps on it however, I want to detect a tap on the second, then the third, etc.

How can I handle mul开发者_JAVA百科tiple touch events? I think I need an onTouchEvent method and in it I need an if-else statement for the first click but I don't know how I can monitor for the subsequent touch events.


It may help you.I always do like this

public void onClick(View v){
if(v==imageView1){

//do ssomething 
}
if(v== imageView2){
//do something
}
if(v==imageView3){
//do something
}

like this u can do according to different button or imageview


I'm assuming the picture is an ImageView inside the main view. Why not just append touchlisteners to each view?


Set the onClickListener for the n+1 view only when nth view is clicked.

Like this

view1.setOnClickListener(new OnClickListener() {
     void onClick(View v) {
        view2.setOnclickListner(new Onclicklistener() {
           void onClick(View v) {
           // add further view's click listeners else do what ever if this 
           // is the last view.
           }
        });
    }

});

Not an elegant solution but should work IMHO.


From your problem statement it seems you have an ordered list of views, each should have a touch listener, but the listener for the second view should not fire unless the listener for the first view has fired first.

This can easily be done by keeping a counter in your activity: private int highestIndexTapped. When a view is tapped, check whether its index is such that index == highestIndexTapped + 1.

If it is, increase highestIndexTapped by 1 and fire the listener. Otherwise either eat the touch event or pass it on to the next part of your pipeline.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜