Differences between android Button ImageButton

Source: Internet
Author: User

BUTTON class structure:

 

From the class structure diagram, we can see that the Button is inherited from TextView, so some attributes of TextView also apply to the Button control.


ImageButton class structure:

 

ImageButton uses an icon to represent some text. It does not have the Android: text attribute. It specifies the icon position by Android: src
Android: src = "@ drawable/back_48"
Other attributes are similar to buttons.

 

 


1. How do I set the button style?
Set Through Android: background

[Java]
<Button android: id = "@ + id/myBtn1" android: text = "Button 1 sets the background style"

Android: layout_width = "fill_parent" android: layout_height = "wrap_content"

Android: background = "# fff000"/>

 


2. How to set the background icon:


[Java]
<Button android: id = "@ + id/myBtn6" android: text = "Button 6 sets the background icon"

Android: layout_width = "wrap_content" android: layout_height = "wrap_content"

Android: textStyle = "bold" android: background = "@ drawable/back_48"

/>


3. How to set the text color of the button
Android: textColor

[Java]
<Button android: id = "@ + id/myBtn2" android: text = "Button 2 font color"

Android: layout_width = "fill_parent" android: layout_height = "wrap_content"

Android: textColor = "# ff0000"/>


 
Nbsp;
4. How to set the button text style
Android: textStyle

[Java]
<Button android: id = "@ + id/myBtn3" android: text = "Button 3 font bold"

Android: layout_width = "fill_parent" android: layout_height = "wrap_content"

Android: textColor = "# ff0000" android: textStyle = "bold"/>


5. How to add a listener registration event for the button
Method 1: setOnClickListener

[Java]
MyBtn4.setOnClickListener (new OnClickListener (){



@ Override

Public void onClick (View v ){

MyBtn4.setText ("setOnclickListener event listener registration successful ");



}

});

 

Practice:
1. How to set the icon position of ImageButton
[Html]
<ImageButton android: id = "@ + id/imgBtn01" android: src = "@ drawable/forward_48"

Android: layout_width = "wrap_content" android: layout_height = "wrap_content"/>


2. How to add a listener to register an event for ImageButton?
Like Button, it can be set in two ways.
Method 1: Use onClickListener
[Java]
ImgBtn01.setOnClickListener (new OnClickListener (){



@ Override

Public void onClick (View v ){



MyTextView. setText ("ImageButton listener event ");

}

});


Method 2: Set Through XML file
[Html]
<ImageButton android: id = "@ + id/imgBtn02" android: src = "@ drawable/back_48"

Android: layout_width = "wrap_content" android: layout_height = "wrap_content"

Android: onClick = "ImageButtonXml"/>

 

1. Set transparency

[Java]
ImgBtn01.setAlpha (50); // sets the transparency.
Author: hjm4702192

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.