> For the complete documentation index, see [llms.txt](https://easonwang.gitbook.io/android/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easonwang.gitbook.io/android/cheng-shi-xiang-guan/onclick.md).

# onClick

有幾種寫法

1.Lambda

```java
        Button b = findViewById(R.id.button1);
        b.setOnClickListener(view -> {
            System.out.println(123);
            new AlertDialog.Builder(c)
                    .setMessage("test")
                    .show();
        });
```

2\.

```
final Context c = this;
mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // do something here
    }
});
```

<https://stackoverflow.com/questions/30752547/what-does-it-mean-that-a-listener-can-be-replaced-with-lambda>
