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.

2nd Nov 2020, 3:27 PM
Mons Joseph
Mons Joseph - avatar
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);
2nd Nov 2020, 6:22 PM
zemiak
+ 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"));
3rd Nov 2020, 3:04 AM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
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
2nd Nov 2020, 3:48 PM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
0
Why does setOnClickListner have so many lines of code within . What does that even mean.
2nd Nov 2020, 3:53 PM
Mons Joseph
Mons Joseph - avatar