Handling multiple view Clicks
hey guys in this tutorial i will show you guys multiple ways to handle the clicks in the view of an android...
1 method:
ImageButton btplus = (ImageButton)findViewById(R.id.btplus); btplus.setOnClickListener( new ImageButton.OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "+", Toast.LENGTH_SHORT).show(); } }); ImageButton btminus = (ImageButton)findViewById(R.id.btminus); btminus.setOnClickListener( new ImageButton.OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "-", Toast.LENGTH_SHORT).show(); } });
2 method:
for implemnting this method your activity class must implement OnClickListener interface then override the onClick class
ImageButton btminus = (ImageButton)findViewById(R.id.btminus); btminus.setOnClickListener(this); ImageButton btplus = (ImageButton)findViewById(R.id.btplus); btplus.setOnClickListener(this); public void onClick(View v) { switch (v.getId()) { case R.id.btplus: Toast.makeText(getApplicationContext(), "Plus is clicked" + "+", Toast.LENGTH_SHORT).show(); break; case R.id.btminus: Toast.makeText(getApplicationContext(),"Minus is clicked" + "-", Toast.LENGTH_SHORT).show(); break; default: break; } }
3 method:
ImageButton btminus = (ImageButton)findViewById(R.id.btminus); btminus.setOnClickListener(listener); ImageButton btplus = (ImageButton)findViewById(R.id.btplus); btplus.setOnClickListener(listener); OnClickListener listener=new OnClickListener(){ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btplus: Toast.makeText(getApplicationContext(), "Plus is clicked" + "+", Toast.LENGTH_SHORT).show(); break; case R.id.btminus: Toast.makeText(getApplicationContext(),"Minus is clicked" + "-", Toast.LENGTH_SHORT).show(); break; default: break; } } });
Comments
Post a Comment