0

I'm trying to understand what is View.OnClickListener().
I have read this site: http://developer.android.com/reference/android/view/View.html, but I cannot understand who is the client and who is the listener.

Please explain in details. Thanks in advance.

4

2 に答える 2

1

From docs:

Interface definition for a callback to be invoked when a view is clicked.

reference

Simply said: So when you implement this, you are able to handle click events for your Views - all widgets as Button, ImageView etc..

When you implement this you have to implement onClick method. When you click on some View, this method is immediately called.

public void onClick(View v) {
   switch(v.getId()) {
      // do your work
   }
}

But don't forget that you have to register your OnClickListener for specific widget

someButton.setOnClickListener(this);

Most likely you need to learn Android basics and i recommend it to you.

Note: You can use Listeners also as anonymous classes

于 2012-07-08T21:17:48.370 に答える
0

これは、View要素がクリックされた場合に通知を受け取りたいクラスに実装するためのインターフェースです。

例えば:

public class FooActivity extends Activity implements View.OnClickListener {

    public void onCreate(...) {
        View v = findViewById(...);
        v.setOnClickListener(this);
    }

    public void onClick(View v) {
        // method which is invoked when the specific view was clicked
    }
}
于 2012-07-08T21:21:23.593 に答える