Java code
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. ImageButton;
Import android. widget. TextView;
/**
* Focus and event processing of ImageButton
*
*/
Public class ActivityMain extends Activity {
Private ImageButton myImageButton;
Private Button myButton;
Private TextView myTextView;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Create three components
MyImageButton = (ImageButton) findViewById (R. id. myImageButton );
MyButton = (Button) findViewById (R. id. myButton );
MyTextView = (TextView) findViewById (R. id. myTextView1 );
// Use OnFocusChangeListener to respond to the onFocus event of ImageButton
MyImageButton. setOnFocusChangeListener (new ImageButton. OnFocusChangeListener (){
Public void onFocusChange (View v, boolean hasFocus ){
// If ImageButton gets the focus onFocus, The ImageButton image will be changed.
If (hasFocus = true ){
// Status change description
MyTextView. setText (& quot; image button status: Got Focus & quot ;);
// Modify the button background image
MyImageButton. setImageResource (R. drawable. iconfull );
} Else {
MyTextView. setText (& quot; the status of the image button is Lost Focus & quot ;);
MyImageButton. setImageResource (R. drawable. iconempty );
}
}
});
// Use onClickListener to respond to the onClick event of ImageButton
MyImageButton. setOnClickListener (new ImageButton. OnClickListener (){
Public void onClick (View v ){
// If ImageButton is in onClick state, the image and description of ImageButton are changed.
MyTextView. setText (& quot; image button status: Got Click & quot ;);
MyImageButton. setImageResource (R. drawable. iconfull );
}
});
// Use onClickListener to respond to the onClick event of the Button
MyButton. setOnClickListener (new Button. OnClickListener (){
Public void onClick (View v ){
// If the Button status is onClick, change the ImageButton Image
MyTextView. setText (& quot; the status of the image button is Lost Focus & quot ;);
MyImageButton. setImageResource (R. drawable. iconempty );
}
});
}
}
Author: Aladdin lamp"