Android method rewrite to Lambda

2450 views android
8

I've want to rewrite the method to Lamda.

CheckCar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
        CheckMotor.setChecked(false);
        et_mileage.setText(String.valueOf(DistanceTextD_Car));
    }
});enter code here

Lamda? CheckCar.setOnClickListener(view->CheckMotor.setChecked(false));

But how to add et_mileage.setText(String.valueOf(DistanceTextD_Car));

answered question

2 Answers

10

CheckCar.setOnClickListener(view-> {
    CheckMotor.setChecked(false);
    add et_mileage.setText(String.valueOf(DistanceTextD_Car));
});

posted this
9

Just change the single expression to a statement block

CheckCar.setOnClickListener(view -> {
    CheckMotor.setChecked(false));
    et_mileage.setText(String.valueOf(DistanceTextD_Car));
});

As the docs say, the arrow token -> is followed by

A body, which consists of a single expression or a statement block.

posted this

Have an answer?

JD

Please login first before posting an answer.