Android development of the project _android

Source: Internet
Author: User

This series of articles will be based on the progress of the project related to the introduction, it will be divided into modules, each module is not dependent on each module is a separate learning content, such as SQLite learning module, contacts contact module and so on.

New Project

I believe that most children's shoes can be customized items, so here, I briefly introduce the process of new Android Studio project.

Eventually, Android Studio will generate 2 Build.gradle files for you, you can choose the Android view to browse files, and according to Google's latest SDK version, we set the project to API 14~ 23, open the Gradle file for your project project, in dependencies

Classpath ' com.android.tools.build:gradle:1.5.0 '

You don't have to care if this version is consistent with me because the default Jcenter warehouse uses HTTPS and the server is abroad, we add buildscript under repositories and allprojects under the repositories

Mavenlocal ()
maven{url ' http://maven.oschina.net/content/groups/public/'}

Its meaning is to search the jar or AAR package in the local warehouse and open source China maven warehouse.

Sub Modules

You'll find that Android Studio will create your demo module by default, which consists of 3 main parts:

    1. Plugin Android Plugin
    2. Android Android code block, which defines the version build information for the app
    3. Dependencies the module's dependent libraries

Because the project involves a lot of modules, each module will correspond to a gradle file, and each Gradle file will have the corresponding Android build information, which is most repetitive, and maintenance is very difficult, therefore, we modify in advance:

subprojects{  
  Configure (':d emo ') {
    apply plugin: ' Com.android.library '
    android {
      compilesdkversion
      buildtoolsversion ' 23.0.2 '
  
      defaultconfig {
        minsdkversion
        Targetsdkversion
        versioncode 1
        versionname "1.0"
      }
      lintoptions {
        abortonerror false
      }
    }
    dependencies {
      testcompile ' junit:junit:4.12 '
      compile ' com.android.support:appcompat-v7:23.0.2 '
    }
  }

So, in your new module, you can completely eliminate the module corresponding to the Gradle file code, is not very convenient?

Demo Module setup

The module is the app that runs on your phone, so the basic skeleton and UI parts need to be written here. The corresponding modules will be shown in the following illustration:

So how do you show the features of these modules?

Here, I would like to introduce you to appcompatactivity and Butterknife and Coordinatorlayout,appbarlayout,collapsingtoolbarlayout,toolbar, The use of Nestedscrollview, of course, also includes the use of Recyclerview,cardview.

Most of them are new components in the Android V7 package that require systematic learning, and butterknife is the Reliance Injection framework of JK great God.

Butterknife the use of the detailed

God horse is butterknife, using annotations, you no longer use the Findviewbyid initialization view. On the details of Butterknife, here is not much to say, then how to use it?

Before using it, you need a powerful plugin from Android studio Zelezny, about installing Plug-ins, you can look through the first article:

Restart Android Studio after Setup completes.

For example, now you can define a button in your activity_main.xml and assign it to its ID. Then open your Mainactivity.java file, move the cursor over the R.layout.activity_main, use the shortcut ALT + Insert key (note the Eclipse Shortcuts I use), and select Generate Butterknife injections, select confirm, the plugin will automatically generate @bind for you, as well as Butterknife.bind (this), just like this:

Butterknife.bind (This)

When you use @bind annotations in an activity or a fragment, you need to add the method to the OnCreate method, meaning that the ID in @bind is automatically found in the layout of the view. So it needs to be added after the Setcontentview method. The special note is that Butterknife.bind (this) does not apply in fragment, you need to use:

public class Fancyfragment extends Fragment {
   @Bind (r.id.button1) Button button1;
   @Bind (R.id.button2) Button button2;

   @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
          view View = Inflater.inflate (R.layout.fancy_fragment, container, false);
          Butterknife.bind (this, view);
          TODO Use fields
          ... return view;
         }

   @Override public void Ondestroyview () {
          super.ondestroyview ();
          Butterknife.unbind (this);
         }
    

You may ask, Butterknife is not using reflection, which can cause performance problems. In fact, you don't have to worry, the code will be generated automatically when the view is loaded, and the auto-generated code looks like this:

public void bind (exampleactivity activity) {
 Activity.subtitle = (android.widget.TextView) Activity.findviewbyid ( 2130968578);
 Activity.footer = (Android.widget.TextView) Activity.findviewbyid (2130968579);
 Activity.title = (Android.widget.TextView) Activity.findviewbyid (2130968577);
}

If you are still not comfortable, you can use the debug mode to view the Bind method.

Listening for bindings

For example, if you have more than one button in your view, you must first initialize the buttons and then add a listener event for each button, then the class implements the OnClick interface, rewrites the interface, and uses the switch method to determine which button was clicked. In fact, you can use Butterknife:

@OnClick (r.id.submit) public
Void Submit (view view) {
 //TODO submit the data to server ...
}

The arguments for are optional:

@OnClick (r.id.submit) public
Void Submit () {
 //TODO submit the data to server ...
}

Define a special type that will automatically convert to you:

@OnClick (r.id.submit) public
void Sayhi (Button button) {
 button.settext ("hello!");
}

So you have multiple buttons:

@OnClick ({r.id.door1, R.id.door2, R.id.door3}) public
void Pickdoor (Doorview door) {
 if Door.hasprizebehind ( ) {
  Toast.maketext (this, "Your win!", Length_short). Show ();
 else {
  Toast.maketext (this, ' Try again ', Length_short). Show ();
 }

Is it super convenient!!!

For more information about butterknife and how to use it, you can view: http://jakewharton.github.io/butterknife/

OK, so much for today, the next one will bring you the construction and Appcompatactivity,coordinatorlayout,appbarlayout,collapsingtoolbarlayout,toolbar of the project, The use of Nestedscrollview!!!

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.