package jp.co.wata.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
int count1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
final Button button1 = new Button(this);
button1.setText("Count");
View.OnClickListener onClickListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
count1++;
button1.setText("Count:" + count1);
}
};
button1.setOnClickListener(onClickListener);
linearLayout.addView(button1,new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
//anonymous class
final Button button2 = new Button(this);
button2.setText("Count");
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
count1++;
button2.setText("Count:" + count1);
}
});
linearLayout.addView(button2,
new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
//Lambda expression
final Button button3 = new Button(this);
button3.setText("Count");
button3.setOnClickListener(v -> {
count1++;
button3.setText("Count:" + count1);
});
linearLayout.addView(button2,
new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
}
}