Imagebutton and button

Source: Internet
Author: User

1. Button Control

The butotn control is mainly used to implement some command operations by registering a listener event. First, put a button in the XML document.

1     <Button2         android:id="@+id/button1"3         android:layout_width="180dp"4         android:layout_height="64dp"5         android:layout_x="45dp"6         android:layout_y="269dp"7         android:background="@drawable/btn01"8         android:text="Button" />
Button

You can set related properties of the control in XML, including layout_width. The Code implemented in the background is

1 public class mainactivity extends activity {2 3 button mybutton; 4 imagebutton myimg; 5 textview; 6 @ override 7 protected void oncreate (bundle savedinstancestate) {8 super. oncreate (savedinstancestate); 9 setcontentview (R. layout. activity_main); 10 mybutton = (button) findviewbyid (R. id. button1); 11 textview = (textview) findviewbyid (R. id. text1); 12 mybutton. setonclicklistener (New onclicklistener () {13 14 @ override15 public void onclick (view v) {16 // The method stub 17 textview automatically generated by todo. settext ("wo lai le"); 18 toast. maketext (mainactivity. this, "Don't worry about me", 5000 ). show (); 19 }}); 20} 21 22 23 @ override24 public Boolean oncreateoptionsmenu (menu) {25 // inflate the menu; this adds items to the action bar if it is present.26 getmenuinflater (). inflate (R. menu. main, menu); 27 return true; 28} 29 30}
Button event

Here, we use findviewbyid () to obtain the button control and implement the setonclicklistener () method for the control. This method can respond to the button click event. Toast. maketext () to implement the pop-up prompt language.

2. imagebutton Control

The imagebutton control can be used as another extension of the button. It can display icons in drawable.

Register the imagebutton control.

1     <ImageButton2         android:id="@+id/imageButton1"3         android:layout_width="60dp"4         android:layout_height="wrap_content"5         android:layout_x="86dp"6         android:layout_y="145dp"7         android:background="@drawable/easyicon_net_24"8         android:src="@drawable/imgbutton" />
Imagebutton

You can set various attributes of the control during registration.

In this article, we will change the status of the imagebutton control to different images. Different images are displayed when you click the mouse. We need to add an XML document to the drawable folder.

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:drawable="@drawable/easyicon_net_24" android:state_pressed="false"></item><item android:drawable="@drawable/syicon_net_24" android:state_pressed="true"></item></selector>

This document is used to register different statuses of imagebutton. It is implemented by setting the attributes of Android: state_pressed.

 

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.