简体   繁体   中英

How to check if a button was clicked in a different class in java?

I have 4 button in 1 (Class A) of my classes. All buttons lead to the same class (Class B), however, I want to set an if restriction depending on what button is clicked in Class A. Here is a sample of my code.

I want to change the value of qid depending on what option is clicked. For example, if the clicked was 5, the if statement should be "qid > 5". If button clicked was 10, the if statement should be "qid > 10". I am not sure if you can do this, but I think if we assign a number to each button that gets transferred to Class B, the the If statements could become "If (code=1 || qid > 5). Again, I'm not certain how to approach this. Any help would be appreciated. Thanks in Advance.

Send:

Intent i = new Intent(this, ToClass.class);
i.putExtra("button", "5");
startActivity(i);

Receive:

Intent intent = getIntent();
int buttonValue = Integer.valueOf(intent.getExtras().getString("button"));
if (qid > buttonValue ) {
    // Do stuff...
}

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