Focus and event processing of ImageButton

Source: Internet
Author: User

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"
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.