0
I'm learning android but this line of code isn't making any sense. Can someone explain this one.
button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextBox.setText(TextBox.getText() + "1"); } }); Ps:button1 and TextBox are the ids that i set for a button and Text input respectively.
4 Answers
+ 2
it is an anonymous class - you can define a single-use class for the object in expression of creating the object.
it's an abbreviated notation for something like this:
class MyListener implements View.OnClickListener {
@Override
public void onClick(View v) {
TextBox.setText(TextBox.getText() + "1");
}
}
// ... then
MyListener listener = new MyListener();
button1.setOnClickListener(listener);
+ 4
You can now use some of the Java 8 features in Android development including lambda expression and method references.
-Setup process
https://developer.android.com/studio/write/java8-support
And you can use lamdas in your projects.
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextBox.setText(TextBox.getText() + "1");
}
});
With lambda expression,
button1.setOnClickListener(v -> TextBox.setText(TextBox.getText() + "1"));
0
setOnClickListener method fires when button with id (button1) was clicked.
new View.OnClickListener
View : View is parent class of all views in android some vies are button textview linearlayout etc
OnClickListener : is an interface with method onClick that fires when someone clicks on button
0
Why does setOnClickListner have so many lines of code within . What does that even mean.