1. OnClickListener:
Two Ways to use OnClickListener Event
- First Step
It is an event in android when you click on button, editText, and more. See example below.
Step 1:
public class Your_ Activity extends Activity implements OnClickListener{
//en your activity extends OnClickListener
Step 2:
btn_search = (Button)findViewById(R.id.btn_search);
btn_search.setOnClickListener(this);
//inside onCreate Method, you can initialize the button. I used button for this event.
Step 3:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn_search:
//do everything here...
break;
}
//Use switch statement to get the ID of a button. Thats All!
- Second Step
Step 1:
btn_search = (Button)findViewById(R.id.btn_search);
//initialize it under OnCreate Method. Or you can use a function for a better programming tutorial.
Step 2:
et_datefrom.setOnClickListener(new OnClickListener() {
//do some code here...
});
//That's it!
2. OnFocusChangeListener():
Step 1:
btn_search = (Button)findViewById(R.id.btn_search);
//initialize it under OnCreate Method. Or you can use a function for a better programming tutorial.
Step 2:et_datefrom.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
//do some code here
}
}
});
This even occur when you hit a button, edittext or etc. Actualy there are many ways to used that like onClickListener also.
No comments:
Post a Comment