KingKonApp
KingKonApp

Reputation: 35

Android OnClickListener Mechanism

I am beginner of Android and java .I cannot understand this portion of my code. May be it is to create an anonymous object. I cannot understand the mechanism of this code.Please help me.

btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
}); 

Upvotes: 2

Views: 498

Answers (3)

Rahul Raina
Rahul Raina

Reputation: 3450

You can write it in this way as well:

public class MainActivity extends ActionBarActivity implements onClickListener{
    Button b,b1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = (Button)findViewById(R.id.btn);
        b1 = (Button)findViewById(R.id.btn);
        b.setOnClickListener(this);
        b1.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        if(arg0.getId() == R.id.btn)
        {
            /*do some work on the button click*/
        }
        else if(arg0.getId() == R.id.btn)
        {
            /*do some work on the button-1 click*/
        }
    }
}

Upvotes: 0

Deepak anand
Deepak anand

Reputation: 67

btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
});

In the above code, btn is object of Button class. First of all you have to declare the object of button and then initialize it. For more detail with example, visit this link http://androidtutorial4u.blogspot.in/2014/04/button-in-android-example.html

Upvotes: 2

Lavekush
Lavekush

Reputation: 6166

enter image description here

The event model, which you saw at its simplest in the preceding example, is quite powerful and flexible. Any number of event listener objects can listen for all kinds of events from any number of event source objects. For example, a program might create one listener per event source. Or a program might have a single listener for all events from all sources. A program can even have more than one listener for a single kind of event from a single event source.

Java have three mechanism for event listing.

1. By Anonymous claases

Android developers often use anonymous inner classes to define specialized listeners, which register callbacks for specific behavior when an event occurs. For example, to listen for clicks on a View control, the developer must call the setOnClickListener() method, which takes a single parameter: a View.OnClickListener object. Developers routinely use the anonymous inner class technique to create, define and use their custom View.OnClickListener, as follows:

Button aButton = (Button) findViewById(R.id.MyButton);
aButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // User clicked my button, do something here!
            }
});

2. Separate class for onclick listener.

    class MyActivity extends Activity {

    public void myMethod() {
       MyClickHandler handler = new MyClickHandler();
        Button[] buttons = getAllOneHundredButtonsAsArray();
        for (Button button : buttons) {
            button.setOnClickListener(handler);
        }
    }

    class MyClickHandler implements View.OnClickListener {
        public void onClick(View v) {
            showToast(((Button) v).getText());
        }
    }
}

Upvotes: 6

Related Questions