Created
June 13, 2012 11:41
implementing View.OnClickListener for multiple button events...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.android.sample.events; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
public class BaseActivity extends Activity implements View.OnClickListener | |
{ | |
// declare variable to represent our components... | |
Button b1,b2,b3,b4; | |
TextView t1; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
// inside this method we reference our buttons, textview and add event listeners... | |
setupViews(); | |
} | |
private void setupViews() | |
{ | |
b1=(Button)findViewById(R.id.b_1_id); | |
b2=(Button)findViewById(R.id.b_2_id); | |
b3=(Button)findViewById(R.id.b_3_id); | |
b4=(Button)findViewById(R.id.b_4_id); | |
t1=(TextView)findViewById(R.id.t_id); | |
b1.setOnClickListener(this); | |
b2.setOnClickListener(this); | |
b3.setOnClickListener(this); | |
b4.setOnClickListener(this); | |
} | |
// this method is called when any of the buttons is clicked... | |
// the text on the TextView is set according the button pressed... | |
@Override | |
public void onClick(View v) | |
{ | |
switch(v.getId()) | |
{ | |
case R.id.b_1_id: | |
t1.setText("Button 1 is pressed"); | |
break; | |
case R.id.b_2_id: | |
t1.setText("Button 2 is pressed"); | |
break; | |
case R.id.b_3_id: | |
t1.setText("Button 3 is pressed"); | |
break; | |
case R.id.b_4_id: | |
t1.setText("Button 4 is pressed"); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment