General document organization structure for Android application development _java

Source: Internet
Author: User

Understand the links between the various files of Android programming and how to use them, and how to understand the structure of the activity file can be seen in the following diagram:

The code is edited in the. java file in Src, and there is a R.java file under the Gen directory, which stores the ID number of various resource files under the Res directory, calling the resource directly in the main file Java, such as R.layout.main, This entry has to be opened in the R.java file, "R" corresponds to the R.java file, "layout" corresponds to the layout class in the R.java file, and "main" corresponds to a static constant declaration in the layout class.

In fact, each file in res automatically generates static constants in the R.java, which is the biggest difference between the Res directory and the assets directory, and the benefit is self-evident that changes to the resource do not have any effect on the code, because the code uses only the ID number.

The first three folders in the Res directory are for picture resources, and usually the same picture has three versions two, high and low resolution.

The fourth folder is a layout file, Main.xml, an activity that corresponds to an XML file, and each activity is registered in Androidmainfest.xml. Default.properties files When learning Java, but also to the code for the repeatable use and modification of the convenience. The first Java Activity program reflects the relationships and use of these files.

The following program tasks: Add a display text and a button

A preliminary understanding of activity: like a window that can display information, and like a container, can hold functional space, such as a button, in a procedural sense, and like a class that can be contacted with other classes (events).

To create the main points of an activity:

An activity is a class, the class name is random, but the activity must inherit the parent class.
Requires a replication OnCreate () method
Each activity should be configured in the Androidmanifest.xml file
Add the necessary controls for the activity
Overall file Code preview:

Myactivity.java file

Package Geeker. myactivity;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.Button;
Import Android.widget.TextView;
public class MyActivity extends activity {
  //member variable declaration
  private TextView mytextview = null;
  Private Button MyButton = null;
  Overriding the OnCreate method automatically generates the public
  void OnCreate (Bundle savedinstancestate) {
    //calling the parent class method, which automatically generates
    Super.oncreate (savedinstancestate);
    Invokes the layout file
    setcontentview (r.layout.main) used by the activity through the ID of the layout file;
    Get the controls added in the layout file through the Findviewbyid () method//
    However, you must define the ID number when you add the control to the layout file,
    //such as: android:id= "@+id/mytextview"
    Mytextview = (TextView) Findviewbyid (R.id.mytextview);
    MyButton = (Button) Findviewbyid (R.id.mybutton);
    Make the display text Mytextview.settext on the control ("This are my
    "!);
    Mybutton.settext ("My A-button"); 
  }

Main.xml file

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent"
  >
<textview 
  android:id= "@+id/mytextview" android:layout_width= "
  fill _parent " 
  android:layout_height=" wrap_content " 
  />
<button
  android:id=" @+id/mybutton "
  android:layout_width= "fill_parent"
  android:layout_height= "wrap_content"
  />  
</ Linearlayout>

R.jar file This file is automatically generated, do not change it yourself

/* auto-generated FILE. Do not MODIFY.
 * This
 class is automatically generated by the
 * AAPT tool from the resource data it found. It
 * Should is modified by hand.
 * *
package Geeker. myactivity;
Public final class R {public
  static final class attr {
  } public
  static final class Drawable {public
    St atic final int icon=0x7f020000;
  }
  public static final class ID {public
    static final int mybutton=0x7f050001;
    public static final int mytextview=0x7f050000;
  }
  public static final class layout {public
    static final int main=0x7f030000;
  }
  public static final class string {public
    static final int app_name=0x7f040001;
    public static final int hello=0x7f040000;
  }
}

Actually go through the process of adding a button to understand the links between the various documents:

1. Open the Main.xml file first, and add a button to the layout

<button
  android:id= "@+id/mybutton"
  android:layout_width= "Fill_parent"
  Wrap_content "
  />

2. In fact, after the completion of the previous step, the compilation run already can see a button buttons, but I want to add text on the button to explain the role of the button, in Java, the program is:
Button BT = New button ();
Bt.settext ("My I-button");

So how do you get the controls that you just added to the main.xml in the. Java source file in the Android program?

For this purpose, this sentence is added to the Main.xml file: android:id= "@+id/mybutton", which makes the R.java file an additional class called ID, which appears in the class, in order to facilitate the invocation of the. java file.

In fact, if you don't add a sentence, the control does not generate an ID number in the R.java file, because adding a file to the Res directory automatically generates an ID number in R.java, and adding a control is only modified in a resource file, so the ID number is not automatically generated.

We can look at the automatically generated ID code in the R.java file:

public static final class ID {public
    static final int mybutton=0x7f050001;
    public static final int mytextview=0x7f050000;
  }

You can then get the control through the Getviewbyid () method in the. java file.

After you get the control, you can do it like the Java program, code like:

Private Button MyButton = null;
MyButton = (Button) Findviewbyid (R.id.mybutton);
Mybutton.settext ("My I-button"); 

In fact, this process only embodies the link between the XML file and the R.java file (through the sentence: android:id= "@+id/mybutton"), and the connection between Java and R.java (through this sentence: Findviewbyid ( R.id.mytextview)). Add code for other file relationships:

The Myactivity.java file is contacted with the Main.xml file through the Setcontentview (R.layout.main) in the Myactivity.java file; manifests, because an activity file corresponds to a layout file.

The connection between the Myactivity.java file and the Androidmanifest.xml file is passed in the Androidmanifest.xml file.

<activity android:name= ". MyActivity "
         android:label=" @string/app_name ">
      <intent-filter>
        <action android:name=" Android.intent.action.MAIN "/>
        <category android:name= android.intent.category.LAUNCHER"/>
      < /intent-filter>
    </activity>

, which illustrates one of the key points of activity creation: Each activity should be configured in the Androidmanifest.xml file.

PS: Ways to get a list of Android files

Sometimes our program needs to go through the Android directory or the global directory to get the files, but when getting files, you may encounter problems that cannot list the files in the folder, which is my problem, for a subfolder to get Listfiles () Returns NULL when it is not allowed to list the contents of the folder. This is because of the security mechanism in Android, because Android inherits the legacy of Linux, there are three types of user permissions for a particular directory-readable, writable, executable; Although we can set permissions on a particular directory, But for subdirectories and subfolders inside the directory, you can set permissions. In other words, the permissions of the subdirectory itself determine the accessibility of the subdirectory, and we need to have a clear understanding of it, so we need to be in the listfiles () when we are finished judging whether it is a directory. Gets the file[] data to determine whether the obtained array is empty, and if it is empty, the folder is inaccessible. The sample code is as follows:

Package net.nowamagic.file;
Import Java.io.File;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Map;
Import Android.util.Log;
  /** * @author * function is used to scan files on SD card * */public class FileScan {private static final String TAG = "FileScan";
    Public hashmap<string, string> getmusiclistonsys (file file) {//is scanned from the root directory log.i (TAG, File.getpath ());
    hashmap<string, string> filelist = new hashmap<string, string> ();
    Getfilelist (file, filelist);
  return filelist; /** * @param path * @param filelist * Note that not all folders can be read, permission issues/private void Getfilelist (File Pat H, hashmap<string, string> filelist) {//If it is a folder if (Path.isdirectory ()) {//Return the data in the folder file[] Fil
      es = Path.listfiles ();
      
      First judge if there is no permission, if there is no permission, then do not execute if (null = files) return;
      for (int i = 0; i < files.length i++) {getfilelist (files[i), filelist); }///If it is a file, join directly
    else{log.i (TAG, Path.getabsolutepath ());
      Processing of a file String FilePath = Path.getabsolutepath ();
      FileName String filename = filepath.substring (Filepath.lastindexof ("/") +1);
    Add Filelist.put (FileName, FilePath); }
  }
  
}

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.