The most basic concept of the MVC pattern is a layered design that encapsulates our code based on a view (view), model (models), controller (director), which is designed to make the code easier to maintain and extend, with a clear structure.
In the previous article, we completed the interface restore of the calculator, but strictly not the real view class because it did not reflect the logic of the view. In this article, we will write the View section of the calculator program, let's go!
(Note: This time in the code comments to write more points, so you can look at the comments section)
First, the Activity
Activity is one of the four components of Android, which is equivalent to the entire interface in the app, and looks like a Web page from the front-end perspective. And what is the essence of Activity? This is a brief description, from Google's big God, that Activity starts with the Context class, to see their descriptive keywords:
Android.content.Context
Interface to global information on an application environment ... It allows access to application-specific resources and classes, as well as up-calls for application-level operation ...
The original context is defined as an abstract class about the application scenario (context), has access to the application-level resources and classes, and encapsulates some application-level methods.
Android.app.Activity
An activity was a single, focused thing that the user can do ... interact with the user ... takes care of creating a window (Fu Ll-screen windows or floating windows) for-place UI ...
Activity is defined as a function class that interacts with users (events) and is responsible for creating features such as windows that load views. It can be said that because of the Context, the Activity has a powerful function.
Before you start writing, introduce an important Java file-R.java, as described earlier, the Gen directory will automatically generate some files required by the system, open R.java:
The R class records all identities through static internal classes such as Attr,color,drawable,id,layout.
(Note: The identity of the R class is automatically generated without modification.)
Second, write our program in OnCreate
Activity has a distinct feature, which is the life cycle. You can imagine the use of the normal application process, sliding from one interface to another interface, and return from the current interface, along with the different stages of the Activity cycle. Open the Mainactivity.java file for the Calculator project:
In front-end programming, the most important thing is to get the Action object (DOM). This is also true in Android, where the Operation object is obtained primarily through ID identification. We first add the following ID identifier to the TextView and Button in Activity_main.xml:
(@+id/{name} means to add the ID of {name} to the R class)
- Textview:@+id/resultoutput
- Button number 0 ~ 9:@+id/operand0 ~ @+id/operand9
- Button Division sign: @+id/operate0
- Button Multiplication Sign: @+id/operate1
- Button minus: @+id/operate2
- Button Plus: @+id/operate3
- Button equals: @+id/operate4
- Button clear: @+id/operate5
, be sure to add the correct ID to the element.
In Android, the Findviewbyid () method is used primarily to obtain operations objects, such as TextView:
In the front-end programming, we can get a series of operation elements through the getElementsByTagName () method, but not so fortunate in Android, there is no such method. What's the quickest way to do that? The answer is yes. We know that Findviewbyid () is passing in a reference value of type int, can you find these reference values in a circular way and get them directly? We divide the Button element into two groups, with the ID of the operand named Operand0 ~ 9. The rest is the operator, named Operate0 ~ 5 (as indicated above). This allows us to:
So, with a TextView and two arrays, we collect all the necessary operational elements.
Third, separating and defining the interface of the View class
Everything seems pretty good from the code above, but this process-oriented approach is one of the reasons why the code is rapidly expanding and difficult to maintain. According to the MVC design idea, the code written above should belong to the view part of the logic, the better way should be encapsulated in the view, implementation details are not known by other classes. We are now following this idea and organizing the code from a new one:
(Note: This is only naïve to use MVC ideas to express intentions, as for the division and the rationality of writing is not explored)
The calculator will consist of two View classes, one for the Caoutputview class to display the results, and one for the Cainputview class for user input. First, set up a package that holds the View class, separating different types of files by package.
Then we create these two View classes:
Next, we open the Cainputview.java file. OK, now let's think of a problem, Cainputview is responsible for interacting with the user, will naturally know what button the user presses, but how to notify the activity user's behavior? This is the concept of the callback mechanism we are ready to contact.
Like, Cainputview to the Activity said: you put "contact" me, the user input I will inform you. "Contact" has many ways of implementing it. This is done in a delegated way, equivalent to the concept of an agent (delegate) in IOS:
Four, write the View class
Now let's continue to write the Cainputview class and put the code logic of the original Activity class into the Cainputview class:
To continue writing the Caoutputview class, the Caoutputview class is simple and only used to display:
Five, using the View class in Activity
Two View has been created and you can now try to use it in the Activity:
Prompt error, because the instantiation of Cainputview, but did not implement the interface, so the prompt mainactivity should implement the Cainputview declaration interface:
The implementation of the Cainputview declaration of the interface will still error, because there is no way to implement the interface declaration method, select "Add unimplemented methods" is automatically added method, such as:
Finally, when Cainputview interacts with the user, we "notify" the activity, and the activity invokes Caoutputview to display the result, and the final code for the Mainactivity class is as follows:
Run the program and click on each button to see if the correct values are displayed:
With layered design, the code in mainactivity is much more concise, and it only needs to know how to use the View class so that it can focus on its own part of responsibility.
Vi., summary
This time the point is more, mainly:
- The design concept of MVC
- Two ways to get an action object
- Types of Java and transformation-related concepts
- Callback Mechanisms and interfaces
- How to use the View class
If the points are not clear, you can look at the comments section, or leave a message to me, I will reply as soon as possible. In addition, I will try to speed up the progress of the update, next week will use the recursive function to write our Model class, please look forward to.
Getting Started with Android: MVC mode (middle)