package com.example.onclicklistener;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView tvFirst,tvFirstWay,tvSecond,tvSecondWay,tvThird,tvThirdWay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvFirst = findViewById(R.id.first);
tvFirstWay = findViewById(R.id.firstWay);
tvSecond = findViewById(R.id.second);
tvSecondWay = findViewById(R.id.secondWay);
tvThird = findViewById(R.id.third);
tvThirdWay = findViewById(R.id.thirdWay);
tvSecond.setOnClickListener(this);
tvSecondWay.setOnClickListener(this);
// First Method
tvFirst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Nawlesh", Toast.LENGTH_SHORT).show();
}
});
tvFirstWay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Bengal", Toast.LENGTH_SHORT).show();
}
});
}
// Second Method
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.second:
Toast.makeText(this, "Kamlesh", Toast.LENGTH_SHORT).show();
break;
case R.id.secondWay:
Toast.makeText(this, "Nawada", Toast.LENGTH_SHORT).show();
break;
}
}
// Third Method
public void tvThird(View view) {
Toast.makeText(this, "Munna", Toast.LENGTH_SHORT).show();
}
public void tvThirdWay(View view) {
Toast.makeText(this, "Boisar", Toast.LENGTH_SHORT).show();
}
}
0 Comments