I am newbie in android and learning first time.
I am making a very simple snippet something like when user clicks the button, the app should show a Toast message.
I have read many tutorials on the web. All tutorials I have seen on the web uses anonymous inner class to implement listeners and their methods.
What I have tried is here:
public class MainActivity extends Activity {
private Button mTrue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTrue= (Button) findViewById(R.id.turebutton);
mTrue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, R.string.correct_toast,Toast.LENGTH_LONG).show();
}
});
}
It works perfectly, but seems difficult in understanding.
I am familiar with Java basics. i have read this and I already know about inner class and anonymous inner class in java.
In java,if we code something like this sample,
public void init(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
showStatus("mouse pressed");
}
});
}
the MouseListener is an interface and then we make instance of MouseAdapter and pass it to call Listener's method on it.
I have some questions:
Why we have passed the instance of interface (
View.onClickListener) here?..i have checked the documentation,View.onClickListeneris an interface not the class.setonClickListeneris looking same likeaddMouseListener. but the conflicting thing is In java, we had toaddListeners andpassinstances of class. here things are inverse.
I am totally wired. please help if anyone can explain well. thanks in advance.
View.added beforeOnClickListener?