Suppose we have an Activity with a lot of views on which OnClickListener
is to be registered.
The most common way to implement this is to let the Activi
simply you using like not implements subclass or not handle a click event just do like this way .
android.view.View.OnClickListener method_name = new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// put your code .
}
};
and handle click event into button ya any type of click event like
button_name.setOnClickListener(method_name);
its work very simply Thanks
I have found using Butterknife makes for clean code. And because it uses code generation (not reflections) it has little performance overhead.
public class ActivityMain extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
}
@OnClick(R.id.button_foo)
void onFoodClicked() {
// Do some foo
}
@OnClick(R.id.button_bar)
void onBarClicked() {
// do some bar
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Chronometer chronometer;
private Button startButton;
private Button stopButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chronometer = findViewById(R.id.chronometer);
startButton =findViewById(R.id.startBtn);
stopButton = findViewById(R.id.stopBtn);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.startBtn:
chronometer.start();
break;
case R.id.stopBtn:`
chronometer.stop();
break;
}
}
}
Your ClickListener
is an inner non-static class the coupling of this 'has-a' is no different than if your class Activity
implemented View.OnClickListener
. This is because your inner ClickListener
requires an instance of ActivityMain
and really can't be reused. I would argue that you're over engineering and aren't actually gaining anything.
EDIT: To answer your question I like to have anonymous View.OnClickListener
for each widget. I think this creates the best separation of logic. I also have methods like setupHelloWorldTextView(TextView helloWorldTextView);
where I put all my logic related to that widget.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button north,south,east,west;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
north.setOnClickListener(this);
south.setOnClickListener(this);
east.setOnClickListener(this);
west.setOnClickListener(this);
}
private void init(){
north = findViewById(R.id.north);
south = findViewById(R.id.south);
east = findViewById(R.id.east);
west = findViewById(R.id.west);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.north:
Toast.makeText(MainActivity.this,"NORTH",Toast.LENGTH_SHORT).show();
break;
case R.id.south:
Toast.makeText(MainActivity.this,"SOUTH",Toast.LENGTH_SHORT).show();
break;
case R.id.east:
Toast.makeText(MainActivity.this,"EAST",Toast.LENGTH_SHORT).show();
break;
case R.id.west:
Toast.makeText(MainActivity.this,"WEST",Toast.LENGTH_SHORT).show();
break;
}
}
}
Here you can create a btnClickListner object and after that you will call that btnCLickLisner object when ever you want to perform the onCLieck actions for buttons..
Let us assume, in my activity i have a 5 to 10 buttons and writing each button separate onclick listner is bad idea. So to over come this,we can use like below..
register your buttons
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
Button button5 = (Button)findViewById(R.id.button5);
Here i am setting the onclick listner to my buttons after click
button1.setOnClickListener(btnClickListner);
button2.setOnClickListener(btnClickListner);
button3.setOnClickListener(btnClickListner);
button4.setOnClickListener(btnClickListner);
button5.setOnClickListener(btnClickListner);
Here is the btnClick Listner implementation
View.OnClickListener btnClickListner = new OnClickListener()
{
@Override
public void onClick( View v )
{
// TODO Auto-generated method stub
if( button1.getId() == v.getId() )
{
//Do Button1 click operations here
}
else if( button2.getId() == v.getId() )
{
// Do Button2 click operations here
}
else if( button3.getId() == v.getId() )
{
// Do Button3 click operations here
}
else if( button4.getId() == v.getId() )
{
// Do Button4 click operations here
}
else if( button5.getId() == v.getId() )
{
// Do Button5 click operations here
}
}
}