您的位置:首页 > 移动开发

Getting Started-Building Your First App

2013-09-20 23:44 417 查看
Creating an Android Project

Create a project with command line tools:

execute:android list targets

execute:android create project -t <target-id> -n MyProjectName -p <path-to-workspace>/MyProjectName -a MainActivtiy -k com.example.mypackage

 

Running Your Application

One fo the most important elements your manifest should include is the <uses-sdk>element.This declares your app's compatibility with different Android version using the
android:minSdkVersion and
android:targetSdkVersion

You should always set the
android:targetSdkVersion
as high as possible.

To run your app from a command line:

Change directories to the root of your Android project,and make sure that you have already installed apache ant tools

execute:ant debug

execute:install bin/MyProjectName-debug.apk

Building a Simple User Interface

Use match_parent is better than fill_parent.

The weight value is a number that specifies the amount of remaining space each view should consume, relative to the amount consumed by sibling views.

In order to improve the layout efficiency when you specify the weight,you should change the width of the EidtText to be zero(0dp).Setting the width to zero improves layout performance because using "wrap_content"as the width
requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.

Starting Another Activity

To respond to the button's on-click event,add the android:onClick attribute to the <Button> element.The android:onClick attribute's value is the name of a method in your activity that the system calls when the user clicks the
button.

In order for the system to match this method to the method name given to android:onClick ,the signature must be exactly as shown.Specifically,the method must:

Be public

Have a void return value

Have a View as the only parameter(this will be the View that was clicked).

In Eclipse ,press Ctrl+Shift+O to import missing classes.

In order for the next activity to query the extra data, you should define the key for your intent's extra using a public constant. So add the EXTRA_MESSAGE definition to the top of the MainActivity class:

It's generally a good practice to define keys for intent extras using your app's package name as a prefix. This ensures they are unique, in case your app interacts with other apps.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息