I'm creating an android app. I have a Dialog, and I want to handle the onCancel() event without using an anonymous class because it's cleaner and there are class variables I need access to from the main Activity class. I'm looking for a way to register for events on an object similar to .NET, where I can handle it in a separate method in the class without the need for an anonymous class.
There is a nice example here in the Event Listeners section.
The first example uses an anonymous class for the listener; the second uses a method inside the Activity. No extra class needed.
TL;DR Here is the code stolen from that page:
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
...
}
You can modify this to use onCancel()
.
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.