[Reading Notes-Android game programming starts from scratch] 3. Commonly Used system controls for Android game development (buttons, Layout, ImageButton)

Source: Internet
Author: User

3.1 Button

The Button control is a Button, mainly used to respond to corresponding events after being clicked.

Add the ID attribute to the component. The definition format is android: id = "@ + id/name". The name here is customized, not an index variable. "@ +" Indicates a new declaration, and "@" indicates a reference, for example:
"@ + Id/TV" indicates a new id, which is a component named TV;
"@ Id/TV" indicates that the component named TV is referenced.

Add Click Event Response to button

If you want to know whether a button is clicked by a user, you need a "Click listener" to listen to the button, and then obtain the click event through the listener, you can determine whether the following button is clicked by the user.
There are two ways to use listeners:
1. Click the listener interface for the current class. The modified source code is as follows:

ButtonProject Activity === (v = "OK button trigger event! "(V =" cancel button trigger event! "

First use the current class to click the listener interface (onClickListner), override and click the listener's abstract function (onClick), and then bind the button to be listened to the listener, in this way, the listener can listen on the bound button to determine whether it is clicked by the user. Once the button is clicked, the listener will automatically respond to The onClick function, and pass in the clicked button (the button is also a view). Finally, you can write the event triggered by the click in The onClick function (because multiple buttons are defined, therefore, The onClick function determines the button matching of the view passed in by the system, so that different buttons cannot handle events ).

 

2. Use an internal class to implement the listener. The source code after modification is as follows:

ButtonProject === btn_submit.setOnClickListener (TV. setText ("OK button trigger event! "TV. setText (" cancel button trigger event! "

In the form of internal classes, you also need to rewrite the abstract function of the listener, and then process the event in onClick. You do not need to judge the view here, because a Button corresponds to a listener.

 


Button official documentation: Http://developer.android.com/reference/android/widget/Button.html

It indicates that if you do not need the OnClickListener listener, you can use the android: onClick attribute in the XML layout button control. Then, call the onClick method you set in the class. The Code is as follows:

  

When you click the button, the Android system calls the selfDestruct method. This method must be public and accept a view as its unique parameter.

  }

 

3.2 Layout

1. Linear layout LinearLayout

Link to the LinearLayout official documentation:Http://developer.android.com/reference/android/widget/LinearLayout.html

2. Relative layout: RelativeLayout

RelativeLayout official documentation address: http://developer.android.com/reference/android/widget/RelativeLayout.html

3. TableLayout

Official TableLayout documents:Http://developer.android.com/reference/android/widget/TableLayout.html

4. Absolute layout AbsoluteLayout

AbsoluteLayout official document address:Http://developer.android.com/reference/android/widget/AbsoluteLayout.html

 

5. FrameLayout

FrameLayout official documentation address:Http://developer.android.com/reference/android/widget/FrameLayout.html

 

3.3 ImageButton

Official ImageButton documentation:Http://developer.android.com/reference/android/widget/ImageButton.html

ImageButton is similar to Button. The difference is that ImageButton can define an image as a Button, and because the image replaces the Button, the effect of pressing and lifting ImageButton needs to be customized.

      
  ImageButtonProject          =        btn_ImageButton.setOnTouchListener(                             (event.getAction()==MotionEvent.ACTION_DOWN) (event.getAction()==MotionEvent.ACTION_UP) 

 

The official documentation for displaying different button statuses mentions that you can configure them in an xml file separately, and then reference them in the layout file. The Code is as follows:

                       

 

 

 

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.