Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 16th, 2012  |  syntax: None  |  size: 3.12 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android - multiple OnClickListener?
  2. public class NavigateActivity extends Activity  implements OnClickListener {
  3.  
  4.     // images
  5.     private ImageView phone;
  6.     private ImageView bookings;
  7.     private ImageView settings;
  8.     private ImageView pictures;
  9.  
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.navigate);
  14.         phone = (ImageView) findViewById(R.navigate.callcenter);
  15.         phone.setOnClickListener(new OnClickListener() {
  16.             public void onClick(View v) {
  17.                 if (v == phone) {
  18.                     AlertDialog alertDialog = new AlertDialog.Builder(NavigateActivity.this).create();
  19.                     alertDialog.setTitle("Attention");
  20.                     alertDialog.setMessage("Etes-vous sur de vouloir appeler le Call center");
  21.  
  22.                     alertDialog.setButton("Oui", new DialogInterface.OnClickListener() {
  23.                        public void onClick(DialogInterface dialog, int which) {
  24.                           Intent callIntent = new Intent(Intent.ACTION_CALL);
  25.                             callIntent.setData(Uri.parse("tel:1232456789"));
  26.                             startActivity(callIntent);
  27.                        }
  28.                     });
  29.  
  30.                     alertDialog.setButton2("Non", new DialogInterface.OnClickListener() {
  31.                            public void onClick(DialogInterface dialog, int which) {
  32.                                dialog.cancel();  
  33.                            }
  34.                         });
  35.                     alertDialog.show();
  36.                 }
  37.             }
  38.         });
  39.  }
  40.  
  41.     @Override
  42.     public void onClick(View v) {
  43.         // TODO Auto-generated method stub
  44.  
  45.     }
  46. }
  47.        
  48. phone.setOnClickListener(this);
  49. bookings.setOnClickListener(this);
  50. settings.setOnClickListener(this);
  51. pictures.setOnClickListener(this);
  52.        
  53. @Override
  54.     public void onClick(View v) {
  55.  
  56.        if(v == phone){
  57.               // your stuff
  58.         }
  59.        else if(v == bookings){
  60.               // your stuff
  61.         }
  62.        else if(v == settings){
  63.               // your stuff
  64.         }
  65.        ese if(v == pictures){
  66.               // your stuff
  67.         }
  68.     }
  69.        
  70. DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
  71.                            public void onClick(DialogInterface dialog, int which) {
  72.                                /*...*/
  73.                            }
  74.                         });
  75.  
  76. phone.setOnClickListener(listener);
  77. bookings.setOnClickListener(listener);
  78. /*...*/
  79.        
  80. img1.setOnClickListener(imgClk);
  81. img2.setOnClickListener(imgClk);
  82. img3.setOnClickListener(imgClk);
  83. img4.setOnClickListener(imgClk);
  84.        
  85. public OnClickListener imgClk = new OnClickListener()
  86.     {
  87.         @Override
  88.         public void onClick(View v)
  89.         {
  90.             if(v == img1)
  91.             {
  92.             }
  93.  
  94.             if(v == img2)
  95.             {
  96.             }
  97.  
  98.            if(v == img3)
  99.             {
  100.             }
  101.  
  102.             if(v == img4)
  103.             {
  104.             }
  105.  
  106.  
  107.         }
  108.     };
  109.        
  110. View.OnClickListener listener = new View.OnClickListener() {..};
  111.        
  112. phone.setOnClickListener(listener);