"Android Programming The Definitive Guide," 3rd edition reading notes

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/gaolh89/article/details/81213044

(A) Chapter 1: Android early development experience

1.android:text property
P11 page

Button assembly having the TextView android: text attribute. This attribute specifies the component to display text.
Please note, Android: text attribute value is not a string value, but a reference to a string resource (stringresource) of . String resource is contained in a separate XML file called strings in (strings.xml), although the text attribute values can be hard-coded set of components, such as android: text = "True", but this is usually not a good idea. Good practice: the text is placed in a separate string resource XML file, and then reference them. This will facilitate the localization of applications (support multiple languages).

2.AppCompatActivity
P13页

AppCompatActivity it is actually a subclass of Activity, to provide compatibility support for the older version of the Android system.

3. Resource and resource ID
P13-14 page

Switch to the Project view of the project. Project view displays all files and directories in the current project.
Expand the directory app / build / generated / sourcelr / debug. Locate the project name and open the package which R.java file, you can see all the current application of the current resource. R.java Android project files in the compilation process is automatically generated , as a warning to the head of the file, do not modify the contents of the file.
After modifying the layout and other resource or string, R.java file is not updated in real time . In addition there is also the code as a hidden file R. java compiler used. When the current code editing area open only R.java file in the application before installation to produce a device or simulator, so only click to run the application in Android Studio, which will be updated.
R.java files are usually relatively large, such as the book GeoQuiz application, only one interface, three buttons, R.java file with comments, is more than 10,000 lines of code.
We look at the comment section R.java file

/* AUTO-GENERATED FILE. DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found. It
 * should not be modified by hand.
 */

According translation: automatically generated file can not be changed.
This class is automatically generated by the tool aapt resource data it should not be manually modified.

4. Set the listener
P17 page

Android application is a typical event-driven type . Unlike the command line or a script, the event-driven application startup, began waiting to happen behavioral events, such as the user clicks a button. (Event can also be triggered by the operating system or other applications , but the events triggered by the user a more intuitive, such as clicking a button.)

Application of a specific event waiting to happen, it can be said that the application is "listening" to specific events. Objects created in response to an event called the listener (listener). Listeners will implement the listener interface (listener interface) of a particular event .

(B) Chapter 2: Android and MVC design pattern

P44 page

There ImageButton android: contentDescription attributes of the property can be easily provided for the visually impaired users to set text in the property value, if the device can access line option is set appropriately, then when the user clicks on the graphical button, the device will read. content property value.

(C) Chapter 3: activity life cycle

1. Before lifecycle wrote a blog about. Click to Jump

2. Device configuration with an alternative resource
P52-53 page

Rotating the device the device configuration changing apparatus is actually a combination of a series of features, used to describe the current state of the apparatus these features are: The screen orientation, screen density, screen size, keyboard type, a base model and language.

In general, to match different device configuration, application will provide different alternative resources for the application of different resolution screen, add multiple sets of the same name (and in a different folder) to the project icon is such a use case .

Screen density device is a stationary device configuration changes can not occur during operation. However, other features may change the screen orientation when the application is running.

When configuration changes occur at run time, there may be more suitable resources to match the new device configuration. So, Android destruction of current activity, find the nearest resource for the new configuration, and then create a new instance of the use of these resources.

…..

When the device is in a horizontal direction, Android will find the layout and use of resources under the res / layout-land directory. In other cases, it will use the default layout resources under res / layout directory.

3. Save the data to respond to device rotation
P56-58 page

First rewrite onSaveInstanceState (Bundle) method.

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_INDEX, mCurrentIndex);
    }

By rewriting onSaveInstanceState (Bundle) method, some of the data stored in the bundle.

The data was then removed in onCreate (Bundle) method:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        if (savedInstanceState != null) {
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
        }
    }

….

Note that the data type is stored and can only be restored Bundle Basic type and can achieve Serializable interface or Parcelable Save the custom class object is not a good idea Bundle because you remove the object may have been useless. Compare a good practice is to save the custom class objects in other ways, and save the identity of the object in the Bundle basic types of data.

(D) Chapter 4: Debugging Android applications

1. Lint Android
P70-71 page

Lint is Android Android application code static analyzer (staticanalyzer). As - the case of a special program, you can not run code under the code error checking. With skillful mastery of the Android framework, AndroidLint can deeply examine the code, the compiler can not identify the problem found. In most cases, AndroidLint check out the issues are worthy of attention.
....

If you want to take the initiative to see all the potential problems in the project, you can choose Analyze → Inspect Code .. menu items manually run the Lint. When asked which part of the test item, select Whole project. Android Studio will run immediately Lint and other static analysis code analyzer start.
Write pictures described here

Inspection is completed, all of the potential problems listed by category. Expand the category Android Lint, Lint can see specific information, as shown below:
to continue can also see more detailed information, including where the problem occurred.
Write pictures described here

(E) Chapter 5: Create a second activity

1.TextView attributes of tools to use
P77 page

<TextView
    android:id="@+id/answer_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"
    tools:text="Answer"
 />

Note TextView for displaying the answer form, its tools and tools: namespace change text attributes rather special namespace can override any of the properties of an organized way, as you can see the effect in the preview... TextView have text properties can provide to its initial value, and therefore the application is running before you know it is probably the way while the application is running, Answer text will not be displayed .

Guess you like

Origin blog.csdn.net/gaolh89/article/details/81213044
Recommended