Reputation: 1117
I got a button that gets the text from some edit texts, and does some calculation on them depending on what option was choose from spinner, so there is a lot of code in on Click and i need to use it 3 times
At the moment my code looks something like this
if (getIntent().hasExtra("blk")) {
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// a lot of code here
}
});
} else if (getIntent().hasExtra("Length")) {
edtNumber1.setText(setLength + "");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// do the same here
}
});
} else if (getIntent().hasExtra("Height")) {
edtNumber2.setText(setHeight + "");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// do the same here
}
});
How do I shorten up this code, I find myself doing this a lot repeating the same code
Upvotes: 1
Views: 142
Reputation: 313
Use a method to do it if you don't want to implement an interface.
setButtonListener(Button btn) {
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
whenButtonGetsClicked();
}
});
}
whenButtonGetsClicked(){
//do your magic
}
However, i recommend using an interface to do it as described by Tanix.7x
Upvotes: 2
Reputation: 1802
To shorten your code, implement OnClickListener and add unimplemented method.
For each button set your event handler like this:
btn.setOnClickListener(this);
Back in your onClick() method do something like this:
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btn:
//TODO Code
break;
case R.id.btn2:
break;
}
}
This should solve your problem.
Upvotes: 1
Reputation: 7521
The most direct way for me:
OnClickListener buttonListener=new OnClickListener() {
@Override
public void onClick(View arg0) {
// a lot of code here
}
}
if (getIntent().hasExtra("blk")) {
btn.setOnClickListener(buttonListener);
} else if (getIntent().hasExtra("Length")) {
edtNumber1.setText(setLength + "");
btn.setOnClickListener(buttonListener);
} else if (getIntent().hasExtra("Height")) {
edtNumber2.setText(setHeight + "");
btn.setOnClickListener(buttonListener);
}
Although if the case were slightly more complex (for example I wanted very similar parametrised functionality) it's often better to use an inner class which implements OnClickListener.
Upvotes: 1
Reputation: 3476
You can make your ActivityClass to implement View.OnClickListener()
Now for each button set onClickListener()
as ->
btn.setOnClickListener(this)
Implement onClickListener as ->
@Override
public void onClick(View v) {
//Do common work here and if you want to do different work then ->
switch(v.getId())
{
case R.id.btn:
//TODO Code
break;
case R.id.something:
break;
}
}
Upvotes: 3
Reputation: 67229
You could modify your Activity to implement OnClickListener
and override onClick()
appropriately.
Then you only need to call btn.setOnClickListener(this);
and you only need to call it once.
If you have many Views that call your Activity's onClick method, you can do a switch on view.getId()
.
Upvotes: 2
Reputation: 40218
As @Luksprog mentioned in comments, create a method that you can reuse:
private void doALotOfWork() {
// do a lot of work here
}
And call it everywhere it's needed:
@Override
public void onClick(View v) {
doALotOfWork();
}
Upvotes: 2