A lambda expression is a concise way of declaring an interface that contains only one method. For example, an anonymous OnClickListener
could have only one method, namely, the onClick
method. So you can replace this anonymous OnClickListener
with a lambda expression.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(view -> textView.setText(R.string.you_clicked));
textView = (TextView) findViewById(R.id.textView);
}
}
No comments