CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren 2017-2018 SPRING Computer Engineering Department CMPE419 AU
Controling Buttons with a Single Listener public class MainActivity extends ActionBarActivity implements View.OnClickListener { Button b1,b2,b3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button1); b2=(Button)findViewById(R.id.button2); b3=(Button)findViewById(R.id.button3); b1.setOnClickListener(this); b2.setOnClickListener(this); b3.setOnClickListener(this); }
public void onClick(View v) { // TODO Auto-generated method stub @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()){ case R.id.button1: Toast.makeText(getApplicationContext(), "Button 1" , Toast.LENGTH_SHORT).show(); break; case R.id.button2: Toast.makeText(getApplicationContext(), "Button 2" , Toast.LENGTH_SHORT).show(); case R.id.button3: Toast.makeText(getApplicationContext(), "Button 3" , Toast.LENGTH_SHORT).show(); }