Button On Click Listener
Solution 1:
You have three options:
common in 1,2) You need to assign an id to each of your buttons in the layout XML file
<Button android:id="@+id/my_button1"
..........
/>
<Button android:id="@+id/my_button2"
..........
/>
1) In the activity's onCreate()
method after setContentView()
you need to set a new OnClickListener
for each button.
publicclassMyActivityextendsActivity {
int a;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButtonmyButton1= (Button) findViewById(R.id.my_button1);
ButtonmyButton2= (Button) findViewById(R.id.my_button2);
myButton1.setOnClickListener( newOnClickListener() {
@OverridepublicvoidonClick(View v) {
// Do what you want here
a = 1;
}
});
myButton2.setOnClickListener( newOnClickListener() {
@OverridepublicvoidonClick(View v) {
// Do what you want here
a = 2;
}
});
}
2) As you see in the first approach, we need to make a new Object from the OnClickListener
for each button. We can mix all that into one OnClickListener
for performance and readability reasons.
publicclassMyActivityextendsActivityimplementsView.OnClickListener {
int a;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButtonmyButton1= (Button) findViewById(R.id.my_button1);
myButton1.setOnClickListener(this);
ButtonmyButton2= (Button) findViewById(R.id.my_button2);
myButton2.setOnClickListener(this);
}
@OverridepublicvoidonClick(View v) {
switch (v.getId()) {
case R.id.my_button_1:
a = 1;
break;
case R.id.my_button_2:
a = 2;
break;
}
}
...
}
3) You don't need to assign an id for this option you just need to assign the method name in the XML and then implement the same method in the activity with the exact same name but it must take a View object as an argument.
<Button...android:onClick="button1Click" /><Button...android:onClick="button2Click" />
and then in your activity just write the methods.
publicclassMyActivityextendsActivityimplementsView.OnClickListener {
int a;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
publicvoidbutton1Click(View v) {
a = 1;
}
publicvoidbutton2Click(View v) {
a = 2;
}
}
That's all your options. I personally prefer number 2.
Solution 2:
You've two options to handle the buttons, from the documentation:
a) Declare and define you method in you java file
inta=0;
finalButtonbutton= (Button) findViewById(R.id.button_id);
button.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View v) {
a = 1;
}
});
b) or define you method in you xml layout:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct"
android:onClick="selfDestruct" />
And then in you java file:
int a = 0;
publicvoidselfDestruct(View view){
a = 1;
}
Solution 3:
First assign the button in yours xml file like below:-
<Button android:id="@+id/button1"
..........
/>
<Button android:id="@+id/button2"
..........
/>
After finding there ids from xml than set the OnclickListener on it like below and do not forget to implement the OnClickListener listener on yours class like public class CLASSNAME extends Activity implements OnClickListener
button1 .setOnClickListener(CLASSNAME.this);
button2.setOnClickListener(CLASSNAME.this);
Than after implements the onclickListener than it will provide you a overrided method like onclick(View v)
@OverridepublicvoidonClick(View v) {
switch (v.getId()) {
case R.id.button1 : //Here i assumes that button1 is the name of button in yours xml which you declare button1// do something on button1 clickbreak;
case R.id.button2 :////Here i assumes that button2 is the name of button in yours xml which you declare button2// do something on button2 clickbreak;
}
}
Solution 4:
There are two ways to perform action on a Button click
the first approach is to set an android:OnClick = "methodName" in the layout (xml) file in the Button code and use that method in the java code to access that Button like
publicvoidmethodName(View v) { // Do what you want to perform on Button click }
the second way to perform some thing behind a button is to access that button in java code and do what you want to do like
Buttonbutton= findViewById(R.id.button); button.setOnClickListener(newView.OnClickListener() { publicvoidonClick(View v) { // Do what you want to perform on that button click } });
Solution 5:
create two buttons in your activity layout and assign some id's to them like this
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button2" android:layout_gravity="center_horizontal|top" />
Then, In your activity class file get the regerence for those buttons and set onclick Listener like below (write everything within
onCreate()
oronResume
method). Inside onClick method do whatever you want.Button myButton1; Button myButton2; int a; protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); myButton1 = (Button) findViewById(R.id.my_button1); myButton1.setOnClickListener( newOnClickListener() { @OverridepublicvoidonClick(View v) { a =1; } myButton2 = (Button) findViewById(R.id.my_button1); myButton2.setOnClickListener( newOnClickListener() { @OverridepublicvoidonClick(View v) { a =2; }
Post a Comment for "Button On Click Listener"