Android Program Development Learning notes series-basics (with source code)

Source: Internet
Author: User

With the increasing popularity of Android and the continuous launch of various applications, I also coincidentally sent a telecom Android system custom machine during the broadband renewal in March, which prompted me to advance the learning plan developed by Android, I also provided a development and testing site (mobile phone), which also enhanced my interest in learning Android program development-other market prospects for the moment, at least I learned, you can doProgramUse it by yourself. In my spare time, I will try my best to keep every blog in a uniform style in the series of Android program development and learning notes. Before I start to share my learning experience, I will list what this blog will mainly talk about-knowledge points, we also strive to give a brief description so that our friends who are learning Android program development can learn more quickly and accurately what you want to know, however, I also know that even if the writing is better, some people may still feel that they have not made it clear or talked about it. I hope you can give more comments and make the series more readable!

Key points of this Article:

1.1 Android Application directory structure, folder and file description

1.2 Basic usage of the button textview Control

1.3 simple value transfer between activities, simple use of intent

1.4 control event binding

1.1 Android Application directory structure, folder and file description

 

I originally wanted to add comments directly in the middle, but it was troublesome and difficult to clearly understand the small space. The following describes the Red areas in detail:

R. Java-- This file is an application resource class, including the control ID, layout, strings, and drawable, to facilitate the use and unified management of resources throughout the application; this class is automatically generated. Do not modify it manually!

In the res folderFolders starting with drawable are respectively placed in images with different resolutions: high (H), low (l), and medium (m, that is to say, to ensure compatibility or better display of applications on machines with different resolutions, a single image should be placed in these three folders with three different resolutions, for example: the application icon or the image used in it. at startup, the application uses an appropriate image based on the resolution of the current machine (client; images placed in the drawable folder are automatically stored in the R. the drawable class of the Java file generates a corresponding variable,CodeAs follows:

Public StaticFinalClassDrawable {
Public StaticFinalIntCat =0x7f020000;
Public StaticFinalIntIc_launcher =0x7f020001;
}

The content View File is stored in the layout folder.. net. the aspx file is similar to the interface layout file, so some may ask: how is the code control class of each content View File associated with it? You need to use setcontentview (R. layout. formactivity) in the oncreate method of each activity class. Set this line of code. Here, Main. XML is the default content View File!

The values folder is a string used in the current application. It is stored as a key-value pair, such as the application name, button, and other prompt text. The advantages of this method are: separating text strings from codes facilitates internationalization. For example, your current application is intended for Chinese users. When you want to add a Japanese or English version, you only need to add the corresponding international strings under the values folder. XML file! The specific practices will be explained in a later blog.

Under the SRC folderThe project name folder of the current application is usually placed in the activity class (which can be understood as the Code class behind the interface form) and other class files.

Androidmanifest. xmlThis file is similar. net web. config File-application configuration file, such as configuration: Application name and icon, minimum supported SDK version, user permission, third-party class library, etc.ArticleThe file content is as follows:

View code

 <?  XML version = "1.0" encoding = "UTF-8"  ?> 
< Manifest Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Package = "Android. firstavd"
Android: versioncode = "1"
Android: versionname = "1.0" >

< Uses-SDK Android: minsdkversion = "15" />

< Application
Android: icon = "@ Drawable/ic_launcher"
Android: Label = "@ String/app_name" >
< Activity
Android: Name = ". Firstavdactivity"
Android: Label = "@ String/app_name" >
< Intent-Filter >
< Action Android: Name = "Android. Intent. Action. Main" />

< Category Android: Name = "Android. Intent. Category. launcher" />
</ Intent-Filter >
</ Activity >
< Activity
Android: Name = ". Formactivity"
Android: Label = "@ String/formactivity_name" >
</ Activity >
</ Application >

</ Manifest >

Package = "android. firstavd" package name of the current application

Android: versioncode = "1" current application version

Android: versionname = "1.0" Name of the current application version

<Application
Android: icon = "@ drawable/ic_launcher" icon of the current application

Android: Label = "@ string/app_name"> name of the current application

<Activity
Android: Name = ". firstavdactivity" Interface Class Name of the form
Android: Label = "@ string/app_name"> name of the interface form

There are two activity declarations (registration),Only the internal <intent-filter> activity is the form interface started by the application by default, and each activity must be declared (registered) in this file; otherwise, an error is reported.!

1.2 Basic usage of the button textview Control

First, add a control to the XML file of the content view, and then obtain the control object based on the control ID in the corresponding activity class. The Code is as follows:

 <  Textview
Android: ID = "@ + ID/TVshow"
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
Android: Text = "@ String/author" />
< Button
Android: ID = "@ + ID/btnchangecontent"
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
/>

 

View code

 Public   Class Firstavdactivity Extends Activity {
Private Textview TVshow;
Private Button btnsendsms;
Private Button btnaction;
Private Button btnchangecontent;
/** Called when the activity is first created. */
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
/**
* Textview TV2 = new textview (this );
Tv2.settext ("nice see you! "+ New date (). gettime ());
Setcontentview (TV2 );
*/
// Set the content view (File) of the current activity)
Setcontentview (R. layout. Main );
TVshow = (textview) This . Findviewbyid (R. Id. TVshow );
Btnaction = (button) This . Findviewbyid (R. Id. btnforward );
Btnaction. settext ("test button ");
Btnaction. settextsize (23 );
Btnaction. settextcolor (color. Green );
Btnaction. setbackgroundcolor (color. Gray );
Btnaction. setonclicklistener ( New Btnactiononclicklistener ());

1.3 simple value transfer between activities, simple use of intent

The following code redirects from firstavdactivity to formactivity and obtains the passed value in formactivity:

Jump button event in firstavdactivity --

 Class Btnactiononclicklistener Implements Onclicklistener
{
@ Override
Public Void Onclick (view v ){
// Todo auto-generated method stub
// Create an intent request object
Intent intent = New Intent ();
// The value to be passed is stored in the intent object as a key-value pair.
Intent. putextra ("user_name", "tom_marker ");
// Set the request source (firstavdactivity) and target (formactivity)
Intent. setclass (firstavdactivity. This , Formactivity. Class );
// Execute request
Firstavdactivity. This . Startactivity (intent );
}
}

Get passed value in formactivity --

 Public   Class Formactivity Extends Activity {
Private Textview TVshow;
@ Override
Protected Void Oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Try {
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. formactivity );
Intent intent = This . Getintent ();
TVshow = (textview) This . Findviewbyid (R. Id. TVshow );
// TVshow. settext (new date (). getminutes ());
TVshow. settext (R. String. weltext + intent. getstringextra ("user_name "));
} Catch (Exception e ){
TVshow. settext ("error:" + E. getmessage ());
}
}
}

1.4 control event binding

There are two ways to bind an event: paste the code.

Method 1

Btnchangecontent. setonclicklistener (NewView. onclicklistener (){
Public VoidOnclick (view V)
{
//Toast prompt control
Toast. maketext (firstavdactivity.This,
"The text in textview has changed. Have you noticed it? ",
Toast. length_long). Show ();
//Change the TVshow text.
TVshow. settext ("android button control binding Click Event" + "\ n" +NewDate (). gettime ());
}
});

Method 2

 btnsendsms. setonclicklistener ( New  btnsendsmsonclicklistener (); 
class btnsendsmsonclicklistener implements onclicklistener
{< br> @ override
Public void onclick (view v) {
// todo auto-generated method stub
/// Create an intent request object
URI uri = Uri. parse ("smsto: // 13227517186");
intent = New intent (intent. action_sendto, Uri);
intent. putextra ("sms_body", "this SMS Test content! ");
firstavdactivity. This . startactivity (intent);
}< BR >}

 

Well, if you have a limited time, let's write it here. It may be a little hasty or inactive. I hope it will be helpful to you, and I hope you can exchange and give more comments!

Download firstavd.rar from the above sample code

Video tutorial learning: http://v.youku.com/v_playlist/f5486194o1p30.html

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.