Android happy Snake Game Development tutorial-02 virtual arrow keys (1) custom controls overview, android-02

Source: Internet
Author: User

Android happy Snake Game Development tutorial-02 virtual arrow keys (1) custom controls overview, android-02

Overview and directory of this series of tutorials: http://www.cnblogs.com/chengyujia/p/5787111.html

1. Introduction to custom controls

In this project, both the main area of the game and the virtual arrow keys are implemented through custom controls. We need to have a simple understanding of custom controls first. In addition, you can better understand the internal mechanism of the built-in controls through the learning of custom controls.

  • What is a custom control?

The controls we usually use (such as buttons and TextView) are built-in controls in the Android system. We can use them directly. However, when the built-in controls are insufficient or cannot meet our needs, we need to make our own controls. A custom control is called a custom control.

  • How do I customize controls?

The built-in controls of Android are directly or indirectly inherited from the class android. view. View.

In Android Studio, click a class and press Ctrl + H to see the inheritance relationship of the class.

The following uses a Button as an example:


In this figure, we can see that the Button inherits from TextView, while the TextView inherits from View. At the same time, there are multiple subclasses under the Button, all of which are subclasses of View.

All the controls that come with Android inherit from the View or View subclass, and the same applies to custom controls.

2. Create a custom control for the virtual arrow key

In the past, there were generally physical direction keys on mobile phones, but now almost none. However, it leaves us with a larger screen space, so that we can use it freely. First, we need to use a custom control to create a virtual direction key.

Next let's do it!

First, create a project named HappySnake.

Create a new class named DirectionKeys and let the class inherit from View.

However, an error is reported:


The reason is that although View has a default constructor without parameters, this constructor is used for internal testing by Google developers. It only has the access permission in the package and is not provided externally. As follows:

    /**
     * Non-public constructor for use in testing
     */
    View() {
        mResources = null;
        mRenderNode = RenderNode.create(getClass().getName(), this);
    }

 

Therefore, our custom control must call the Public constructor In a View in its own constructor. Here, the parameters in our constructor must also match those in the View, because these parameters are required by the system box.

Move the cursor to the red line of the Error. Press the shortcut key Alt + Enter to see the error fix prompt:


Follow the prompts to select the first item to create a constructor that matches the parent class:


As you can see, there are four constructor methods in the View, 4th of which are the constructor methods with four parameters starting from Android5.0.

The first and second constructor methods are commonly used. The first one is used to create the control instance using the new keyword in the code, the second is called when the system creates an instance in the XML layout file.
Only the second constructor method is used in this project, but write the first two methods. Writing is not complex. Just press Ctrl and select the desired one. Click "OK.


package net.chengyujia.happysnake;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

/ **
  * Virtual direction keys on the screen
  * Created by ChengYuJia on 2016/8/19.
  * /
public class DirectionKeys extends View {
     // The construction method with only one parameter is called when we create an instance through the "new" keyword in the program.
     public DirectionKeys (Context context) {
         super (context);
     }

     // The construction method with two parameters is called when the system creates an instance in the XML layout file.
     public DirectionKeys (Context context, AttributeSet attrs) {
         super (context, attrs);
     }
} 

 

If you do not use custom attributes, you do not need to modify the constructor.

So far, this custom control has no substantive content, and the article is not easy to edit. Let's continue in the next article ~~~


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.