- Android - multiple OnClickListener?
- public class NavigateActivity extends Activity implements OnClickListener {
- // images
- private ImageView phone;
- private ImageView bookings;
- private ImageView settings;
- private ImageView pictures;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.navigate);
- phone = (ImageView) findViewById(R.navigate.callcenter);
- phone.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- if (v == phone) {
- AlertDialog alertDialog = new AlertDialog.Builder(NavigateActivity.this).create();
- alertDialog.setTitle("Attention");
- alertDialog.setMessage("Etes-vous sur de vouloir appeler le Call center");
- alertDialog.setButton("Oui", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- Intent callIntent = new Intent(Intent.ACTION_CALL);
- callIntent.setData(Uri.parse("tel:1232456789"));
- startActivity(callIntent);
- }
- });
- alertDialog.setButton2("Non", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- });
- alertDialog.show();
- }
- }
- });
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- }
- }
- phone.setOnClickListener(this);
- bookings.setOnClickListener(this);
- settings.setOnClickListener(this);
- pictures.setOnClickListener(this);
- @Override
- public void onClick(View v) {
- if(v == phone){
- // your stuff
- }
- else if(v == bookings){
- // your stuff
- }
- else if(v == settings){
- // your stuff
- }
- ese if(v == pictures){
- // your stuff
- }
- }
- DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- /*...*/
- }
- });
- phone.setOnClickListener(listener);
- bookings.setOnClickListener(listener);
- /*...*/
- img1.setOnClickListener(imgClk);
- img2.setOnClickListener(imgClk);
- img3.setOnClickListener(imgClk);
- img4.setOnClickListener(imgClk);
- public OnClickListener imgClk = new OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- if(v == img1)
- {
- }
- if(v == img2)
- {
- }
- if(v == img3)
- {
- }
- if(v == img4)
- {
- }
- }
- };
- View.OnClickListener listener = new View.OnClickListener() {..};
- phone.setOnClickListener(listener);