Anonymous new View.OnClickListener() can be replaced with lambda
时间: 2024-02-06 14:11:04 浏览: 552
Dr-Anonymous.github.io:orthosam.com
That's correct. Starting from Java 8, anonymous classes can be replaced with lambda expressions in certain cases, including instances of single-method interfaces like `View.OnClickListener`. The syntax for using a lambda expression in this case would be something like:
```
view.setOnClickListener(v -> {
// Do something when the button is clicked
});
```
This code is equivalent to using an anonymous class like:
```
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do something when the button is clicked
}
});
```
Using a lambda expression can make the code more concise and easier to read.
阅读全文