简体   繁体   中英

Two onClickListeners and interaction between them

Lets say I have two onClickListeners, A and B, and some buttons asigned to them. Is there a way for each clicked button that "belongs" to onClickListener A to listen, wait or whatever until next button is clicked and if it's from onClickedListener B to do some code after that, it's not important what, and if it's from onClickListener A to stay put and do nothing but wait next click and also wait like the previous button and so on?

Ok so I edited my answer to kind of do what the other question asked. Let me know if it makes sense. I won't do the work for you, so this is a simple example that kind of shows you what the logic is.

//This is how you set the TAG so you have it. But I know you use getTag, so I am not sure     
//what you are using it for.
a1 = (Button) findViewById(R.id.bA1);
a2 = (Button) findViewById(R.id.bA2);
b1 = (Button) findViewById(R.id.bB1);
b2 = (Button) findViewById(R.id.bB2);
a1.setTag("A");
a2.setTag("A");
b1.setTag("B");
b2.setTag("B");


View.OnClickListener listener = new View.onClickListener() {
    public void onClick(View view) {
        String tag = (String)view.getTag();
        if(tag.equals("A")
            //example on one B button with assumed TAG of "B"
            someButton.setOnClickListener(listener);
            //deregister all your A Views, but again, I'll do one as an example
            //in this case I'll quickly just deregister the A button or view just clicked
            view.setOnClickListener(null);
        else
            if(tag.equals("B")) {
                /*I guess do your thing here but deregister all the B views again
                and register all the A views again*/
                someButton.setOnClickListener(null);
                otherAButton.setOnClickListener(listener);
            }
    }
});

So I make some assumptions to shorten the code up. For example. Set all your A View s with the "A" tag using the setTag method, vice versa with the B View s. Also I assume you want the A views to start off listening to clicks, so when the Activity or Fragment is first launched, thats where you set listener to the A views via setOnClickListener .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM