android sdk sample for sdk找不到怎么使用

Android中文API
中文(中国)
Русский
To help you understand some fundamental Android APIs and coding practices, a variety of sample
code is available from the Android SDK Manager.
To download the samples:
Launch the Android SDK Manager.
On Windows, double-click the SDK Manager.exe file at the root of the Android SDK
directory.
On Mac or Linux, open a terminal to the tools/ directory in the
Android SDK, then execute android sdk.
Expand the list of packages for the latest Android platform.
Select and download Samples for SDK.
When the download is complete, you can find the samples sources at this location:
&sdk&/platforms/&android-version>/samples/
You can easily create new Android projects with the downloaded samples, modify them
if you'd like, and then run them on an emulator or device.从Android-sdk自带的例子快速学习Android--贪吃蛇
学习一门新语言,最好的办法是读源码.学习Android最好的办法,就是学习android自 带的SDK中的例子.比如通过APIdemos这个例子,能够快速的查看各种页面控件的使用方法,也可以自己修改一下,马上在模拟器中查看效果.初学
Android时我最常做的一件事情,就是到这里来调试各种页面控件,找到满意的效果后,copy粘贴代码到自己的项目中去.
为了学习Android SDK Samples 中的项目,记得以前直接import,import
exsiting projects就可以,不知道为什么这次(Eclipse4.2平台)导入的时候,总是出现一些奇怪问题,在网上搜索,学习才知道:
一、正确的方法:
File-&New-&the-&Android-&Android
Sample Project-&Target Name(勾选) 具体例子
前提是事先安装了给版本API的Sample
二、不正确的方法是:
File-&New-&the-&Android-&Android
projects from existing code--&对应的SDK例程。
用此方法选中导入的项目,发现缺少R文件,网上查了一下解决方法如下:
先Clean项目,再重新Build即可。
可是我做了之后,仍然没法生成R文件,求大侠指教该怎么办?
在 preference-&android-&buid中设置。找到Default debug&
Keystore的地址,然后把debug.keystore文件给删除了。然后刷新一下就可以了&
下面我们看一下SDK中一个游戏入门的源码:贪吃蛇.&&
SDK中自带例子在\samples\android-版本\& 目录下,比如我机器上的贪吃蛇在:
D:\android\android-sdk-windows\android-sdk-windows\samples\android-8\Snake目录.
&把这个项目导入到Eclipse当中,即可在模拟器中运行并学习代码。
&贪吃蛇, 打开了手机游戏的先河。 是NOKIA公司在发展手机游戏上的一次成功尝试。让手机游戏玩家们爱不释手,引起了手机游戏的开发风暴,各个手机厂商纷纷加入这个阵营。手机游戏开发从此拉开了序幕。
Android, OPhone
OS作为手机开发的后起之秀,肯定不会错过这份蛋糕的分享。它不仅解决了以住手机游戏的不足(屏幕分辨率小,内存少)的毛病。而且还在手机游戏软件开发上提供更全,更新的API ,更大的内存,而且还可以用JAVA的泛形,提高他的开发速度。现在我们就以ANDROID
SDK 里面自带的贪吃蛇作为例子,去看看这个贪吃蛇是怎么去开发。
开发过手机游戏的人就知道手机开发的三要素:画布(用来绘画游戏的画面)键盘事件,实时刷新。我们知道一般的游戏画面都是由地图,精灵(由游戏的主角,怪物组成),那我们现在就看看贪吃蛇是怎样他的地图的:
整个工程主体也就3个类:Snake,SnakeView,TitleView。Activity,View,Handler是Android
SDK提供的基础类,Coordinate,RefreshHandler是2个辅助类,也是SnakeView类中的内部类。其中,Coordinate就相当于是一个点的坐标(x,y),RefreshHandler的主要功能是将RefreshHandler对象绑定某个线程并与给它发送消息。Snake类是这个游戏的入口点,TitleView类负责游戏的绘画,SnakeView类负责对游戏控制操作的处理。类的结构在上图中已经清晰说明,主要接口解析如下:
TitleView extends View 继承的接口
onSizeChanged()
在视图大小改变的时候调用,比如说手机由垂直旋转为水平 onDraw()
在视图需要重画的时候调用,比如说使用invalidate刷新界面上的某个矩形区域 Snake extends TitleView
继承的接口
onKeyDown()
监听用户键盘操作,并处理这些操作新增加的接口
restoreState()/saveState()
&&& 恢复/保存游戏的数据
重新计算当前游戏的数据然后具体分析整个游戏的框架,任何一个游戏都需要有个引擎来推动游戏的运行,最简化的游戏引擎就是:在一个线程中While循环,检测用户操作,对用户的操作作出反应,更新游戏的界面,直到用户退出游戏。先解析下RefreshHandler这个辅助类的作用,RefreshHandler继承至Handler,其主要作用就是把RefreshHandler与当前线程进行绑定,然后就可以直接给线程发送消息并处理消息,有一点需要强调的是:Handle对消息的处理都是异步。RefreshHandler在Handler的基础上增加sleep()接口,其主要作用就是每隔一个时间段后给当前线程发送一个消息;而handleMessage()则就是在接受消息后,让界面对当前的游戏状态作出处理,
  一、实现游戏的界面 :TileView.java文件
  1、 先声明用来存放绘画图像的X,Y轴的位置的数组:
  private int[][] mTileG//
  2、 编写存放图片索引用图片的X,Y轴位置;
  public void setTile(int tileindex, int x, int y)
  mTileGrid[x][y] =
  3、调用以上的方法以循环的方式位置数组赋值以及图片的索引,
  private void updateWalls() {
  for (int x = 0; x & mXTileC x++)
  setTile(GREEN_STAR, x, 0);//设置顶部的界线的位置
  setTile(GREEN_STAR, x, mYTileCount - 1);//
设置底部界线的
  for (int y = 1; y & mYTileCount - 1; y++)
  setTile(GREEN_STAR, 0, y);/设置左边的界线的位置
  setTile(GREEN_STAR, mXTileCount - 1,
y);/设置右边的界线的位置
  4、重写VIEW 类里面的方法。 把界线画出。
  public void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  for (int x = 0; x & mXTileC x += 1)
  for (int y = 0; y & mYTileC y += 1)
  if (mTileGrid[x][y] & 0) {
  canvas.drawBitmap(mTileArray[mTileGrid[x][y]],
  mXOffset + x * mTileSize,
  mYOffset + y * mTileSize,
  mPaint);
  同上可见: 地图其实就是由图片数组拼直面成的。 面图片又是通过他的图片索引找到,并在mTileGrid[x][y],获取他们的位置索引来确定图片的位置。 这样在一个手机的页面就形成了,简单吧。
//TileView.java原代码如下
com.example.android.
import android.content.C
import android.content.res.TypedA
import android.graphics.B
import android.graphics.C
import android.graphics.P
import android.graphics.drawable.D
import android.util.AttributeS
import android.view.V
public class TileView extends View {
&&& protected
static int mTileS
&&& protected
static int mXTileC
&&& protected
static int mYTileC
&&& private
static int mXO
&&& private
static int mYO
&&& private
Bitmap[] mTileA
&&& private
int[][] mTileG
&&& private
final Paint mPaint = new Paint();
&&& public
TileView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.TileView);
mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
a.recycle();
&&& public
TileView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.TileView);
mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
a.recycle();
&&& public void
resetTiles(int tilecount) {
&&& mTileArray =
new Bitmap[tilecount];
&&& protected
void onSizeChanged(int w, int h, int oldw, int oldh) {
mXTileCount = (int) Math.floor(w / mTileSize);
mYTileCount = (int) Math.floor(h / mTileSize);
mXOffset = ((w - (mTileSize * mXTileCount)) / 2);
mYOffset = ((h - (mTileSize * mYTileCount)) / 2);
mTileGrid = new int[mXTileCount][mYTileCount];
clearTiles();
&&& public void
loadTile(int key, Drawable tile) {
Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tile.setBounds(0, 0, mTileSize, mTileSize);
tile.draw(canvas);
mTileArray[key] =
&&& public void
clearTiles() {
for (int x = 0; x & mXTileC x++) {
&&&&&&&&&&&
for (int y = 0; y & mYTileC y++) {
&&&&&&&&&&&&&&&
setTile(0, x, y);
&&&&&&&&&&&
&&& public void
setTile(int tileindex, int x, int y) {
mTileGrid[x][y] =
&&& public void
onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int x = 0; x & mXTileC x += 1) {
&&&&&&&&&&&
for (int y = 0; y & mYTileC y += 1) {
&&&&&&&&&&&&&&&
if (mTileGrid[x][y] & 0) {
&&&&&&&&&&&&&&&&&&&
canvas.drawBitmap(mTileArray[mTileGrid[x][y]],
&&&&&&&&&&&&&&&&&&&
&&& mXOffset + x
* mTileSize,
&&&&&&&&&&&&&&&&&&&
&&& mYOffset + y
* mTileSize,
&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&
&&&&&&&&&&&
  苹果的位置就是更简单了,他是随机生成的, 而且必须在现在蛇的位置相对远距离:
  看看他的代码:
  private void addRandomApple() {
  Coordinate newCoord =
  boolean found =
  while (!found) { //
  // Choose a new location for our apple
  // 随机生成新的X,Y位置
  int newX = 1 + RNG.nextInt(mXTileCount - 2);
  int newY = 1 + RNG.nextInt(mYTileCount - 2);
  newCoord = new Coordinate(newX, newY);
  boolean collision =
  int snakelength = mSnakeTrail.size();
  for (int index = 0; index &
index++) {
  // 检查一下是存放的位置是否合理流
  if (mSnakeTrail.get(index).equals(newCoord)) {
  collision =
  found = !
  if (newCoord == null) {
  Log.e(TAG, "Somehow ended up with a null
newCoord!");
  mAppleList.add(newCoord);// 添加到新苹果的列表中,
2) 实现键盘事件:
  键盘主要起操作作用, 在任何的手机游戏中键盘都是起重要的用,要本游戏中, 他就是起控制蛇的行走方向: 现在我们分析他的代码:
  if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
  if (mDirection != NORTH) {
  mNextDirection = SOUTH;
  return (true);
  if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
  if (mDirection != EAST) {
  mNextDirection = WEST;
  return (true);
  if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
  if (mDirection != WEST) {
  mNextDirection = EAST;
  return (true);
  以上的代码大家一看就明白, 就是通过判断那个键按下, 然后再给要走的方向(mDirection)赋值。
  以下的代码就是通
  switch (mDirection) {
  case EAST: {
  newHead = new Coordinate(head.x + 1, head.y);
  case WEST: {
  newHead = new Coordinate(head.x - 1, head.y);
  case NORTH: {
  newHead = new Coordinate(head.x, head.y - 1);
  case SOUTH: {
  newHead = new Coordinate(head.x, head.y + 1);
  从以上的键盘代码我们可以看得出,程序中 是通过触发来改变坐标(+1,-1)的方式来改蛇头的方向, 可见坐标在游戏编程中的作用, 这个也是根据手机的屏幕是点阵的方式来显示, 所以坐标就是一个定位器。 在这里大家可能还有一个疑问。 就是就这个蛇什么能够以“7”字形来移动行走, 其实我们稍微仔细观察一下就知道了,在这里面, 他们也是通过坐标的传递来实现的, 只要把头部的坐标点依次赋给下一个点, 后面的每一个点都走过了头部所走过的点,而蛇的头部就是负责去获取坐标,整个蛇的行走起来就很自然和连贯。 坐标的方向变换又是通过判断那个方向按键的按下来改变的, 这样一来, 键盘的作用就发挥出来了。
  蛇吃苹果又是怎样去实现?上面我所说到的坐标就起了作用。在蛇所经过的每一个坐标, 他们都要在苹果所在的(ArrayList mAppleList = new ArrayList())坐标集里面集依次判断,若是坐标相同,那个这个苹果就被蛇吃了 。
在J2ME中,刷新都是在canvas中通过调用线程结合repaint()来刷新,
他们使线程不断去循环,去调用canvas, 笔者在android 入门时也曾经想用J2ME的模式用在android 中,结果报异常了, 为什么呢? 很多人 认为Dalvik虚拟机是一个Java虚拟机,因为Android的编程语言恰恰就是Java语言。但是这种说法并不准确,因为Dalvik虚拟机并不是 按照Java虚拟机的规范来实现的,两者并不兼容;同时还要两个明显的不同: Java虚拟机运行的是Java字节码,而Dalvik虚拟机运行的则是其专有的文件格式DEX(Dalvik Executable)。所以在以前JAVA 里面能使用的模式,
可能在android 里面用不起来
。在自带的例子里面他是通过消息的机制来刷新的。而在android的消定义比较广泛, 比如,手机的暂停, 启动, 来电话、短信,键盘按下,弹起都是一个消息。总的来说, 事件就是消息;只要继承Handler类就可以对消息进行控制,或者处理, 根据具体情况进行具体处理:
  class RefreshHandler extends Handler {
  //响应消息。
  public void handleMessage(Message msg) {
  SnakeView.this.update();// 重要页面
  SnakeView.this.invalidate();刷新页面
  // 向外提供人工的调用消息的接口,
  public void sleep(long delayMillis) {
  this.removeMessages(0);//注消消息
  // 添加消息,
  sendMessageDelayed(obtainMessage(0),
delayMillis);
  刷新就是这样简单的实现了。 所开发出的游戏也是挺简单。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。如何导入android中的sample例子到eclipse中 -
- ITeye博客
如何导入android中的sample例子到eclipse中 AndroidEclipseWindowsBlog.之前文章《Android SDK 2.2 和ADT插件开发环境安装过程》中提起过下载sample包,而今想把sample导入到eclipse中,却发现使用【File】-【Import...】-【General】-【Existing Projects into Workspace】,选择【Select root directory】,单击【Browse...】,选择“D:\android-sdk_r06-windows\samples\android-8”,结果显示:No projects are found to import
后使用如下方法即可。
使用【File】-【Others...】-【Android】-【Android Project】,单击【Next】,选中【Create project from existing sample】单选框,此时Samples复选框可选,选择某个sample,单击【Finish】即可,此后会在eclipse的Package explorer中看到:
最后会发现在D:\android-sdk_r06-windows\samples\android-8\ApiDemos会多出如下三个文件(.classpath、.project、default.properties):
rexueboy2011
浏览: 2356 次
来自: 北京This feature requires the Standard edition. You are running the Trial edition
or your site domain is not associated with your license key.
Please visit
to purchase an upgrade or add your domain.
This feature requires the Standard edition. You are running the Trial edition
or your site domain is not associated with your license key.
Please visit
to purchase an upgrade or add your domain.
This feature requires the Standard edition. You are running the Trial edition
or your site domain is not associated with your license key.
Please visit
to purchase an upgrade or add your domain.
This feature requires the Standard edition. You are running the Trial edition
or your site domain is not associated with your license key.
Please visit
to purchase an upgrade or add your domain.
The Data Springs Product Forums is an excellent resource where you can find solutions to questions previously encountered by other users.
Each product has its own category to help you narrow your search.
It also offers forums for enhancement requests, user guides and general information.
Data Springs can offer additional training through our
Premium Support hours. The training classes can either be held onsite or via web meetings which can be organized according to your preferences.
Data Springs strives to deliver excellent support for our customers. There are multiple ways of accessing support that are outlined below.
Premium Support is offered for additional requirements beyond what is covered in Standard Support. Find out more details below...
This feature requires the Standard edition. You are running the Trial edition
or your site domain is not associated with your license key.
Please visit
to purchase an upgrade or add your domain.
Beta Springs is a web site dedicated to quality control and improvement of the Data Springs products. Please visit this site and request access to setup and test any product specific issue or to review fully functional products before purchase.
RESX Translate is an easy to use service for providing translation services within Visual Studio and
ASP.NET resources files (.RESX Extension).
RESX Spell Check is an easy to use service for providing spell check services within Visual Studio and
ASP.NET resources files (.RESX Extension).
This feature requires the Standard edition. You are running the Trial edition
or your site domain is not associated with your license key.
Please visit
to purchase an upgrade or add your domain.
Check out recent press releases for Data Springs products, events, and media relations.
An online diary and web log from staff and customers for premium DotNetNuke resources, Data Springs Modules, SharePoint Web Parts, Forum Flow, iPhone/Android Mobile Apps, and Data Springs Services.
Get current news and events the easy way! Select the newsletters you would like to receive or enter your email address to view/modify your subscription settings.
This feature requires the Standard edition. You are running the Trial edition
or your site domain is not associated with your license key.
Please visit
to purchase an upgrade or add your domain.
Popular Tags...
DNN Modules
SnowCovered Top Sellers
Frustrated over the lack of customization for your user's registration fields? Dynamically setup your DNN Portal with custom registration fields, layout, questions, and other core integration options......
Ultra Video Gallery is a brother product of Ultra Media Gallery,
UVG allows you to upload videos in various format and automatically encode them to flv or H264 format, you also can add videos from internet or record live videos from your webcam.
Build high performance, completely customizable data-entry forms and views driven by your DNN and external databases. New built-in tools make it a snap to quickly create data entry forms, data views, and even database tables. Plus, add your own HTML, CSS, Javascript, SQL commands, stored procedures,
The most advanced DotNetNuke shopping cart on the planet.
Easy to use e-Commerce, Secure Shopping Cart Software and SEO friendly.
B2C / B2B Ecommerce Sites.
One stop solution for events calendar and events registration! FREE DOWNLOAD is available now!
Recently Viewed...
<a href='/dnnsearchresults.aspx'DNNSearchResults
Quick login...
Or... now make it easy with Facebook Integration
Creating your first Android application ...
&#160; A Brief Guide for Creating your first Android Application &#160; Tip Calculator
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; Table of Contents &#160; 1 ) Welcome To Android - 3 2 ) System Requirements - 6 3 ) Downloads - 7 4 ) Creating a Dummy Application - 8 5 ) Writing Tip Calculator - 11 6 ) What’s Next - 20 &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; Welcome to Android! According to the official google release “Android is a software stack for mobile devices that includes an operation system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language. Features :- · Application Framework enabling reuse and replacement of components · Dalvik virtual machine optimized for mobile devices · Integrated browser based on the open source Webkit engine · Optimized graphics powered by a custom 2D 3D graphics based on the OpenGL ES 1.0 specification · SQLite for structured data storage · Media support for common audio, video and still image formats · GSM Telephony · Bluetooth, EDGE, 3G and WIFI · Camera, GPS, compass and accelerometer · Rich development environment “ Android Architecture The following picture shows the components of Android Operation System. &#160;
Applications Android will ship with a set of core applications including an email client, SMS program, calendar, maps, browser, contacts, and others. All applications are written using the Java programming language. Application Framework Developers have full access to the same framework APIs used by the core applications. The application architecture is designed to simplify the any application can publish its capabilities and any other application may then make use of those capabilities (subject to security constraints enforced by the framework). This same mechanism allows components to be replaced by the user. Underlying all applications is a set of services and systems, including:
A rich and extensible set of
that can be used to build an application, including lists, grids, text boxes, buttons, and even an embeddable web browser
that enable applications to access data from other applications (such as Contacts), or to share their own data A , providing access to non-code resources such as localized strings, graphics, and layout files A
that enables all applications to display custom alerts in the status bar An
that manages the life cycle of applications and provides a common navigation backstack
For more details and a walkthrough of an application, see below... Libraries Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework. Some of the core libraries are listed below:
System C library - a BSD-derived implementation of the standard C system library (libc), tuned for embedded Linux-based devices Media Libraries - based on PacketVideo's OpenCORE; the libraries support playback and recording of many popular audio and video formats, as well as static image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG Surface Manager - manages access to the display subsystem and seamlessly composites 2D and 3D graphic layers from multiple applications LibWebCore - a modern web browser engine which powers both the Android browser and an embeddable web view SGL - the underlying 2D graphics engine 3D libraries - an implementation based on OpenGL ES 1.0 APIs; the libraries use either hardware 3D acceleration (where available) or the included, highly optimized 3D software rasterizer FreeType - bitmap and vector font rendering SQLite - a powerful and lightweight relational database engine available to all applications
Android Runtime Android includes a set of core libraries that provides most of the functionality available in the core libraries of the Java programming language. Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. Dalvik has been written so that a device can run multiple VMs efficiently. The Dalvik VM executes files in the Dalvik Executable (.dex) format which is optimized for minimal memory footprint. The VM is register-based, and runs classes compiled by a Java language compiler that have been transformed into the .dex format by the included "dx" tool. The Dalvik VM relies on the Linux kernel for underlying functionality such as threading and low-level memory management. Linux Kernel Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. The kernel also acts as an abstraction layer between the hardware and the rest of the software stack. System Requirements So rather than wasting time on learning the theoretical aspects we would rather like you to get your hands dirty and create your first app with Google Android. But you need the following things ready with you before starting the development. This tutorial assumes that your OS is Windows XP but you can follow the procedure for other OS easily. Just download the relevant files from the download locations specified. System and Software Requirements :- To develop android app you need to have suitable environment as discussed below :- Supported Operating Systems · Windows XP or Vista · Mac OS X 10.4.8 or later (x86 only) · Linux (tested on Linux Ubuntu Dapper Drake) &#160; Supported Development Environments:
Eclipse IDE
3.3 (Europa), 3.4 (Ganymede)
plugin (included in most Eclipse IDE packages)
(optional, but needed for the Android E included in )
(JRE alone is not sufficient)
(optional) Not compatible with Gnu Compiler for Java (gcj)
Other development environments or IDEs
(JRE alone is not sufficient)
1.6.5 or later for Linux and Mac, 1.7 or later for Windows Not compatible with Gnu Compiler for Java (gcj)
\ Downloads and Development Settings 1 ) Download the google android sdk from
And unzip the files into c:\tools\android 2 ) Download Ant from
And unzip the files into c:\tools\ant 3 ) Download JDK from
And install it on c:\tools\java 4 ) Download DroidDraw from
( Not Necessary but it will be easier to create the GUI using this ) and unzip the files into c:\tools\droiddraw 5) Create two environment variables ANT_HOME and JAVA_HOME and point them to c:\tools\ant and c:\tools\java respectively. Also add c:\tools\ant\bin & c:\tools\java\bin into your path variable. You can create the variables by right clicking on My Computer -& Properties -& Advanced -& Environment Variables.
Be careful when you are editing your path. You should add the paths at the end of already existing path value by separa Creating A Dummy Application Please follow this steps and you will have a simple google android app which will print the message “Hello, World” on your mobile screen. 1 ) Run emulator.exe ( c:\tools\android\tools)
You should have the default screen visible. Keep the emulator running and goto Command Prompt and enter the following commands to create and upload dummy android app to our dummy phone. c:\tools\android\tools&activitycreator.bat --out tipcalc com.android.tipcalc c:\tools\android\tools&cd tipcalc c:\tools\android\tools\tipcalc&ant c:\tools\android\tools\tipcalc&..\adb install bin\tipcalc-debug.apk
The activitycreator.bat file will create all the basic stub files needed to create a basic android app.
Ant will compile the program created by activity creator into binary format which can be run on android phone.
Adb install will upload the binary application on the phone.
This is how your screen should look like :-
Now a simple android app has been created, compiled and uploaded onto our phone emulator. You can go and run that app. It should greet you with the famous “Hello, World “ Message.
Now we will modify this sample app and create our tip calculating application. Don’t forget to delete the uploaded app before installing new app. It is necessary to delete the uploaded app. You can delete it by running this commands. c:\tools\android\tools\tipcalc&..\adb shell #cd data/app #rm com.android.apk #exit You can use ls command to have a look at the app installed by user. Writing Tip Calculator Now we will create a simple tip calculating application very similar to one over here
First we will design our GUI. You can use droiddraw to design the gui.
The droiddraw will generate xml code. You have to replace the code in tipcalc\res\layout\main.xml with the generated code. Old main.xml file contents :- &?xml version="1.0" encoding="utf-8"?& &LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" & &TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, tipcalc" /& &/LinearLayout& New main.xml file contents :- &?xml version="1.0" encoding="utf-8"?& &AbsoluteLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="/apk/res/android" & &TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, tipcalc" /& &TextView android:id="@+id/widget28" android:layout_width="99px" android:layout_height="17px" android:text="Amount of Bill" android:layout_x="40px" android:layout_y="32px" & &/TextView& &TextView android:id="@+id/widget29" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Percentage to Tip" android:layout_x="40px" android:layout_y="82px" & &/TextView& &TextView android:id="@+id/widget30" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Number of People" android:layout_x="40px" android:layout_y="132px" & &/TextView& &TextView android:id="@+id/widget31" android:layout_width="wrap_content" android:layout_height="18px" android:text="Tip Amout" android:layout_x="40px" android:layout_y="262px" & &/TextView& &TextView android:id="@+id/widget32" android:layout_width="wrap_content" android:layout_height="18px" android:text="Total Per Person" android:layout_x="40px" android:layout_y="352px" & &/TextView& &TextView android:id="@+id/widget33" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Total To Pay" android:layout_x="40px" android:layout_y="302px" & &/TextView& &Button android:id="@+id/btncalculate" android:layout_width="87px" android:layout_height="wrap_content" android:text="Calculate" android:layout_x="40px" android:layout_y="182px" & &/Button& &Button android:id="@+id/btnreset" android:layout_width="86px" android:layout_height="wrap_content" android:text="Reset" android:layout_x="140px" android:layout_y="182px" & &/Button& &EditText android:id="@+id/txtbillamount" android:layout_width="wrap_content" android:layout_height="35px" android:text="100" android:textSize="18sp" android:layout_x="200px" android:layout_y="22px" & &/EditText& &EditText android:id="@+id/txtpercentage" android:layout_width="51px" android:layout_height="36px" android:text="10" android:textSize="18sp" android:layout_x="200px" android:layout_y="72px" & &/EditText& &EditText android:id="@+id/txtpeople" android:layout_width="wrap_content" android:layout_height="39px" android:text="1" android:textSize="18sp" android:layout_x="200px" android:layout_y="122px" & &/EditText& &TextView android:id="@+id/txttipamount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_x="200px" android:layout_y="262px" & &/TextView& &TextView android:id="@+id/txttotal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_x="200px" android:layout_y="302px" & &/TextView& &TextView android:id="@+id/txtperperson" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_x="200px" android:layout_y="352px" & &/TextView& &/AbsoluteLayout& Now we will also write the program which will use this GUI to create the tip calculation functionality. In the tipcalc\src\com\android\tipcalc.java file you will find this code ( which was automatically created by activitycreator.bat . Old tipcalc.java contents :- package com. import android.app.A import android.os.B public class tipcalc extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } Replace the above code with the following code . New tipcalc.java contents :- package com. import android.app.A import android.os.B import android.widget.EditT import android.widget.TextV import android.widget.B import android.view.V public class tipcalc extends Activity { private EditT private EditT private EditT private TextV private TextV private TextV private B private B private double billamount = 0; private double percentage = 0; private double numofpeople=0; private double tipamount = 0; private double totaltopay = 0; private double perperson = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initControls(); } private void initControls() { txtbillamount = (EditText)findViewById(R.id.txtbillamount); txtpeople = (EditText)findViewById(R.id.txtpeople); txtpercentage = (EditText)findViewById(R.id.txtpercentage); txtperperson=(TextView)findViewById(R.id.txtperperson); txttipamount=(TextView)findViewById(R.id.txttipamount); txttotal=(TextView)findViewById(R.id.txttotal); btncalculate = (Button)findViewById(R.id.btncalculate); btnreset = (Button)findViewById(R.id.btnreset); btncalculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }}); btnreset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }}); } private void calculate() { billamount=Double.parseDouble(txtbillamount.getText().toString()); percentage=Double.parseDouble(txtpercentage.getText().toString()); numofpeople=Double.parseDouble(txtpeople.getText().toString()); tipamount=(billamount*percentage)/100; totaltopay=billamount+ perperson=totaltopay/ txttipamount.setText(Double.toString(tipamount)); txttotal.setText(Double.toString(totaltopay)); txtperperson.setText(Double.toString(perperson)); } private void reset() { txtbillamount.setText(""); txtpeople.setText(""); txtpercentage.setText(""); txtperperson.setText(""); txttipamount.setText(""); txttotal.setText(""); } } Now we will add an icon for our app.
Modify AndroidManifest.xml so that this line is changed from &application android:label="@string/app_name"& To &application android:icon="@drawable/icon" android:label="@string/app_name"& Now create a directory drawable in tipcalc\res and put icon.png file there. Now compile our new application again by running C:\tools\android\tools\tipcalc&ant Upload the app to the emulator using adb install command and your first google android app is ready for launch. Don’t forget to remove the previous uploaded application first. In Action
What’s Next &#160; So your first android application is now running successfully on the emulator. You should go through the documents provided along with the SDK to learn more about android. There are also few sample applications provided ( in samples ) directory. Compile them, upload them and try to understand them. Initially try modifying few things here and there and once you feel confident you can start with full-fledged development. You should also go through online forums and learn from others. Hopefully you will be creating great android apps in near future. Don’t forget to share them with us. Wishing you Good Luck. &#160; &#160; Download Article Source Code Below (Must be registered user, !) &#160; &#160;
Feedback Comments
i want to create the simple project.
date---weight × weight + weight =weight
+anonymous&&&DNNForumArticle&&&8/17/ PMUsers Still Can't Add a ThreadHello. We followed your instructions but users still can't add a threat. Also, viewers can't even register! Perhaps that's part of the problem...???Eshrath&&&AndroidArticle&&&1/15/ AMhai...nice explantion for beginners to understand.....senthilkumar&&&AndroidArticle&&&1/10/ AMcreating android appsthanks. its very good to learn about basic for android appHemant&&&AndroidArticle&&&12/15/ PMNice contentReally you have provide very nice data for user and developer. thx a lot.
can i use some example of this for my blog
http://android-solution-sample.blogspot.in/
it have also some basic android example and tutorial.zakeer mohammed&&&AndroidArticle&&&12/6/ AMtipcalcgetting an errors in that one venkat&&&AndroidArticle&&&11/20/ PMokhibarry&&&AndroidArticle&&&11/18/ PMpromotional videos
Hey Guys - I am sure youo know
that while you offer an amizing
product and solution - as a
consumer it can get pretty
confusing . I work at a company
called promotionalvideos.us we do..
.PROMOTIONAL VIDEOS! Ther is no
better way to sum up a complicated
business than with a promotional
video. People just love seeing a
promotional video on a home page,
facebook page, or even as a footer
attached to email. Check out some
of the videos that we have done
our website www.promotionaldiveos.us.
Get in contact with me to hear about pricing.
hiren patel&&&AndroidArticle&&&10/26/ PMnicereally good oneThippasandra&&&AndroidArticle&&&9/7/ AMGoodInteresting
Note: Required fields are marked with a
all others are optional.
Enter the code shown above into the box below.
SharePoint Web Parts
All Data Springs Web Parts Support WSS 3.0, SharePoint 2007, and SharePoint 2010 Frameworks
Please select license option for each web part you wish to purchase.&We highly recommend the SharePoint Bundle to get all Data Springs Web Parts for maximum value! & &
Data Springs Sharepoint Bundle
Best Value!
The Bundle gives you all 5 web parts in one package at a very attractive price! Best Value! We think you will be very happy with the SharePoint bundle and great price discounts you will receive. With your purchase all of the web parts below will be included.
Random Image Web Part
With Random Image for Sharepoint 2007, you can select multiple images to display randomly when the web part loads...
Stock Quote Web Part
Giving your site visitors relevant information is critical. With the Data Springs Stock Web Part you can provide your users with up to date financial information
Dynamic Image Rotator Web Part
Who would have thought? Adobe Flash(R) with Sharepoint! The FIRST and ONLY image rotation web part for Sharepoint using Flash Technology from Adobe!
The Dynamic Image Rotator displays selected images and then rotates between the images. Several extended and optional features allow you to select the time to rotate each image, fade between
SharePoint Charts Web Part
The MOSS Chart Web Part is a web part built by Data Springs for the purpose of rendering several chart types based on data from a SharePoint list on a MOSS 2007 or WSS 3.0 Site
Dynamic News Ticker Web Part
Provide current news items with a user-friendly news ticker for your Sharepoint Portal. With millions of web sites offering information you need a fun way to display information and the solution is Flash News Ticker....
Tailored Text Web Part
Tailored Text Web Part allows you to add text/html to your web site that can be different for anonymous users, registered users,
and even individual users specifically.
Dynamic Views Web Part
Dynamic Views is an excellent tool to: Personalization allows you to go the extra mile in communicating or connecting one to one with your clients. When it comes to technology and web site content, you now have the power to leverage this personalization directly with your users on your DotNetNuke(R) site
Dynamic Login Web Part
Your site content isn't vanilla, so why is your portal's login? Add pizazz and functionality with Dynamic Login! Use custom templates, localization, redirection rules for various roles and many more features!
Join our mailing list...
First Name
Email Address
Recent Blogs...
DNN Modules

我要回帖

更多关于 android sdk怎么使用 的文章

 

随机推荐