【郭林专刊】Android学习四之APIDemos

本文介绍了如何通过Android自带的APIDemos学习Android开发,包括API实例、文档使用及自定义Demo添加方法。详细解释了如何在Eclipse中启动APIDemos、浏览功能、学习代码和文档,并提供了在API库中添加自定义Demo的步骤。
先前一直不知道到写一些什么,因为Android自带的APIDemos做得非常好,都是那些真正的高手们写的,而且文档也写的非常漂亮,我个人觉得很多东西都可以直接拿来用,在上边改,然后成为我们自己的东西,说得好听叫“站在巨人的肩膀上”,说得难听点叫“窃取”。反正不过了,学为先

打开Eclipse,点击Android SDK and AVD Manager


打开后选中一个我们已经建好的AVD


启动,有些人的机器可能反应慢一点,给他一点时间,喝杯茶

进入界面后左上角就是APIDemos


单击


大致浏览一下,我这就不去文档找详细的归纳了,昨天看了一本书《Android Application Development》,里面有一些总结,但是这本书在写的时候还没NFC模块,恰巧被张翼飞看到了,说是做传感这一块的,最好的例子是世博会上手机刷票,我到百科里查了一下是:

NFC是Near Field Communication缩写,即近距离无线通讯技术。由飞利浦公司和索尼公司共同开发的NFC是一种非接触式识别和互联技术,可以在移动设备、消费类电子产品、PC 和智能控件工具间进行近距离无线通信。NFC 提供了一种简单、触控式的解决方案,可以让消费者简单直观地交换信息、访问内容与服务。(http://baike.baidu.com/view/917495.htm

可惜点进去没有预想的效果,估计是要在Eclipse先模拟一个发射器



App

这个目录下的范例是演示如何创建一些程序级别的元素,比如Activity、警告、对话框、Intent和Service等。


Content

这个目录下的范例是演示如何从一个普通文件、资源文件和XML文件中读取有用的信息


里面的演示结果第一眼可能会觉得不好看,但功能是有的

Graphics

这个目录里包含许多图形方面的范例,比如位图的各种操作、图像的截取、图层的各种操作和OpenGL的使用等


打开OpenGL ES




里面是非常漂亮的动画,大家可以基于这些做很多更漂亮的东西

Media

这里包含AduioFx、MediaPlayer、和VideoView的示例



NFC

目前位置没有一点头绪,有朋友对此理解甚深,可一起分享


OS

这里演示的是如何调用系统服务,有Morse Code 、Rotation Vector 、Sensors 、SMS Messaging。

实不相瞒,我就认识这最后一个是短信发送服务



Text

这里包含文本的使用范例


Views

这里很多视图组件的范例,在这里可以找到这些组件的使用方法以及如何设定各种参数



这里就大致介绍完了这些APIDemos的功能、作用,具体应用依情况而定。

这些例子的源代码在Android安装目录下的sampls文件下


打开


这里是很多版本的标号,我用的是android2.3,所以他的版本号是10


打开


源代码都在这里了。

那总不能就这么去读代码啊,我们还是要借助文档,毕竟这么好的文档放在不用可惜了


里面讲的太详细了,我如果去班门弄斧,实在是惹人耻笑。不过我还是会把自己学习的经验拿出来一起分享。

最后再说一点,就是如果你觉得每次都做自己的Demo都要建个project,在屏幕上都显示一个图标太难看了,可以把自己写的Demo放到APIDemos里。

我们用一个例子说明,现在要做的是在Views下面添加一个ToastTest

具体方法如下:

首先,打开: android安装路径\samples\android-10\ApiDemos\AndroidManifest.xml


代码太多了,我就不贴出来了

用Eclipse新建一个project


选择android2.3.3

包名一定要写:com.example.android.apis,因为要对应上

在layout文件夹下新建一个toast_test.xml文件


代码如下

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutandroid:id="@+id/RelativeLayout01"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. xmlns:android="http://schemas.android.com/apk/res/android">
  6. <TextViewandroid:id="@+id/TextView01"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="Guessmyfavoritecolor:"
  10. />
  11. <RadioGroupandroid:id="@+id/RadioGroup01"
  12. android:layout_below="@id/TextView01"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. >
  16. <RadioButtonandroid:id="@+id/redButton"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="redButton"
  20. />
  21. <RadioButtonandroid:id="@+id/greenButton"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="greenButton"
  25. />
  26. <RadioButtonandroid:id="@+id/blueButton"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="blueButton"
  30. />
  31. </RadioGroup>
  32. </RelativeLayout>
<?xml version="1.0" encoding="utf-8" ?> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Guess my favorite color:" /> <RadioGroup android:id="@+id/RadioGroup01" android:layout_below="@id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/redButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="red Button" /> <RadioButton android:id="@+id/greenButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="green Button" /> <RadioButton android:id="@+id/blueButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="blue Button" /> </RadioGroup> </RelativeLayout>在src目录下新建一个Activity,包名是:com.example.android.apis.view,因为要放到Views下面显示


代码如下:



  1. packagecom.example.android.apis.view;
  2. importcom.example.android.apis.R;
  3. importandroid.app.Activity;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.widget.RadioButton;
  7. importandroid.widget.Toast;
  8. publicclassToastTestextendsActivity{
  9. /**Calledwhentheactivityisfirstcreated.*/
  10. @Override
  11. publicvoidonCreate(BundlesavedInstanceState){
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. finalRadioButtonredButton=(RadioButton)findViewById(R.id.redButton);
  15. //添加一个监听器
  16. redButton.setOnClickListener(newView.OnClickListener(){
  17. publicvoidonClick(Viewv){
  18. Toast.makeText(ToastTest.this,"OOOOH,RED",Toast.LENGTH_SHORT)
  19. .show();
  20. }
  21. });
package com.example.android.apis.view; import com.example.android.apis.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.RadioButton; import android.widget.Toast; public class ToastTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final RadioButton redButton = (RadioButton) findViewById(R.id.redButton); // 添加一个监听器 redButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(ToastTest.this, "OOOOH,RED", Toast.LENGTH_SHORT) .show(); } });
把这两个文件放到相应的目录下:

android安装路径\samples\android-10\ApiDemos\res\layout

android安装路径\samples\android-10\ApiDemos\src\com\example\android\apis\view

最后修改AndroidManifest.xml文件:android安装路径\samples\android-10\ApiDemos

把新建的Activity添加到这个文件里,应该是添加在TextSwitch1之后

  1. <activityandroid:name=".view.TextSwitcher1"
  2. android:label="Views/TextSwitcher">
  3. <intent-filter>
  4. <actionandroid:name="android.intent.action.MAIN"/>
  5. <categoryandroid:name="android.intent.category.SAMPLE_CODE"/>
  6. </intent-filter>
  7. </activity>
  8. lt;spanstyle="color:#ff0000;"><activityandroid:name=".view.ToastTest"
  9. android:label="Views/ToastTest">
  10. <intent-filter>
  11. <actionandroid:name="android.intent.action.MAIN"/>
  12. <categoryandroid:name="android.intent.category.SAMPLE_CODE"/>
  13. </intent-filter>
  14. </activity>
  15. </span>
  16. <activityandroid:name=".view.ImageButton1"
  17. android:label="Views/ImageButton">
  18. <intent-filter>
  19. <actionandroid:name="android.intent.action.MAIN"/>
  20. <categoryandroid:name="android.intent.category.SAMPLE_CODE"/>
  21. </intent-filter>
  22. </activity>
<activity android:name=".view.TextSwitcher1" android:label="Views/TextSwitcher"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.SAMPLE_CODE" /> </intent-filter> </activity> <span style="color:#ff0000;"><activity android:name=".view.ToastTest" android:label="Views/ToastTest"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.SAMPLE_CODE" /> </intent-filter> </activity> </span> <activity android:name=".view.ImageButton1" android:label="Views/ImageButton"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.SAMPLE_CODE" /> </intent-filter> </activity>然后运行,很遗憾,我没成功,但方法肯定是这样的。我找了很久,也没见办法,如果哪位找出问题,可以留个言


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值