Android开发笔记汇总


 

1.Android学习笔记一:Android开发环境搭建和第一个应用程序

 

 

Android开发环境搭建分为SDK安装和IDE安装,我们使用的IDE为Eclipse,那么还需要在Eclipse中安装Android的开发插件ADT。 
    首先,下载Android SDK,网址为:http://developer.android.com/sdk/index.html,打开后看到如下界面,选择SDK选项卡,下载适合操作系统平台的SDK版本即可。当前版本为r18,下载zip包后解压即可。 
 
    将下载好的zip包解压到本地硬盘文件系统中,如下所示: 
 
    我们将SDK解压到E盘根目录下,得到如下的文件结构,之后需要下载Aandroid的SDK API和文档,驱动等内容。点击SDK Manager.exe,打开SDK管理器,程序会自动从Google网站抓取更新信息,更新完毕,我们可以得到如下内容: 
 
    当前最高版本为Android 4.0.3(API 15),那么我们根据需要下载各个不同的版本,也就是说,所需开发的最低Android版本。出于向后兼容的设计,那么我们只需下载最低的版本,即可在任何比当前版本高的Android平台中运行程序,我们选择API 10,也就是Android 2.3.3,勾选后,点击右侧install package进行下载安装。 
    将SDK下载安装完成后,我们下载Eclipse开发环境,到Eclipse官网下载最新版(当前是Indigo 3.7.2)的程序:http://www.eclipse.org/downloads/,选择适合自己系统平台的安装包。 
 
    下载完成后将Eclipe解压到本地硬盘的文件系统上,打开后安装Eclipse的ADT插件,插件安装在Eclipse的Install New Software中完成,点击Eclipse菜单栏的Help,选择Install New Software,插件的下载地址为:https://dl-ssl.google.com/android/eclipse/ 
 
    将Developer Tools下的四项全部勾选,进行安装。之后根据提示一步一步安装即可。安装完成后,在Eclipse的工具栏会看到如下图标: 
 
    它对应了Android SDK目录下的SDK Manager和AVD Manager,首次在Eclipse中运行SDK Manager需要设置Android的SDK目录所在位置,或者先在Eclipse的Preferences中进行设置。如下图所示,设置SDK Location为Android SDK的所在路径。在这里也可以看到我们下载好各个的Android开发版本。 
 
    设置好后,我们先创建一个AVD,也就是Android虚拟设备,用来模拟运行项目。打开AVD Manager,我们可以看到我们所创建的AVD信息都存储在C:\Users\<用户名>\.android\avd下(Windows 7环境下)。 
 
    选择新建,打开设置面板,设置名称,为Android_2.3.3,目标就是Android 2.3.3,API Level 10,之后设置SD卡的容量大小,这里我们设置为2GB 
 
    创建好后,我们可以到AVD信息所在目录,看看配置内容: 
 
    我们可以在这里修改相关内容,来更新AVD参数,我们将AVD的长宽加以调整,不需要显示的太大,那么修改文件: 

Ini代码  

1. hw.lcd.density=240  

2. sdcard.size=2G  

3. skin.path=350x500  

4. skin.name=350x500  

5. hw.cpu.arch=arm  

6. abi.type=armeabi  

7. vm.heapSize=24  

8. hw.ramSize=256  

9. image.sysdir.1=platforms\android-10\images\  


    下面,我们就可以创建Android项目,进行开发了。下面创建第一个Android项目:HelloAndroid: 
 
    点击下一步,选择Android的最低开发版本,这里选择Android 2.3.3(API 10): 
 
    点击下一步,设置应用程序名称,程序包名,一个必要的Activity和最小的SDK版本(10),设置完成后,点击完成: 
 
    在这里,我们先不创建与之对应的Test项目,先创建Aandroid项目。创建好后,我们得到如下的程序结构: 
 
    先不去考虑这些结构都是做什么用的,我们先将HelloAndroid程序运行起来。在项目上点击右键,选择Run AS,在选择Android Application,来启动模拟器运行程序。首次运行模拟器需要相当长的一段时间,这也和系统运行环境有关,之后我们可以不用关闭模拟器而继续编写代码,然后再运行就不会重新启动模拟器了,速度会快一些。首次启动时,可以看到虚拟器的启动信息如下: 
 
    进入第二阶段,ANDROID字体会变大,说明准备即将就绪。 
 
    启动完成后,模拟器会直接运行我们的程序HelloAndroid,那么我们就会看到如下的显示,说明程序运行成功。
 
    同时,我们可以在Eclipse中打开LogCat视图来查看运行日志。 
 

 

 

 

 

 

 

 

 

2. Android学习笔记二:第一个应用程序的深入说明

 

 

第一个程序已经在模拟器中运行起来了,但是我们只是有了一个感性的认识,对程序结构没有说明,下面我们来对这个程序来做一些深入的说明。 
    首先我们还是从代码结构来说起: 
 
    src目录下存放的是Java源代码,这和所有的Java项目都是一样的,没有特别之处。这里我们的包声明为org.ourpioneer。 
    gen目录下是Aandroid自动控制生成的文件,这里面最重要的一个就是R.java,其中会自动放置我们添加的所有资源和组件,比文字信息,如图片信息,视图控件信息,布局信息等。我们不能修改该文件的任何内容,否则程序会引发严重错误。这里多说一点就是我们编写的程序不会直接和资源,组件文件打交道,都是通过R.java中的信息来获取的。 
    Android 2.3.3是说我们构建Android项目使用的版本号,要说明一点这里是最低版本,也就是说我们构建的程序不能在低于2.3.3版本的Android设备上运行。在这里我们也可以看到我们引用Android类库的位置。 
    Android Dependencies是我们项目引用的依赖,这里仅有一个,可以点看它的内容,作为程序类库上的依赖,这里没有可多说的地方。 
    assets表示资源,也就是程序资源文件存放的地方,这些资源比如图片,音乐,文字和配置信息等。但是可以看到这个文件夹是空的,说明我们没有使用,原因是我们现在一般使用res文件夹来存放这些信息了,下面会说说到。 
    bin目录,不用多说,存放的是我们项目编译之后的文件,同时在这里也可以找到生成的apk包,那么就可以在Android设备上使用该apk来进行安装。 
    res目录是我们真正存放资源文件的地方,根据命名来看,以drawable开头的目录,肯定存放的是图片的文件夹。hdpi存放高清图片,ldpi存放低分辨率图片,mdpi存放中等分辨率图片,xhdpi存放的就是超高清的图片了。它们针对的是不同分辨率的Android设备。我们创建的项目中,这四个图片文件夹下都有内容相同的一个图片(ic_launcher.png),打开后发现它们的分辨率是不同的。 
    res下的layout目录存放的是布局管理器的配置信息,values目录存放的是所有的文字信息,也就是用于显示的文字和布局的配置是分离的,便于项目的维护。layout下的main.xml会自动反映到R.java中,如果我们再在layout目录下创建一个布局文件,那么这个文件名也会自动反映到R.java中。 
    res中的values下的文件都是资源文件,如果我们继续新建,都是resources类型的文件,而在resources中的string类型值会被自动反映到R.java中。resources有string,color,style等子类型,它们也都会自动反映到R.java中,因为这些都是我们程序中的资源。如果将它们的name属性修改或者将它们删除,那么R.java中的内容也会自动被修改或移除,这都是同步进行的。 
    下面从strings.xml说起,我们打开看看这个文件: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <resources>  

3.     <string name="hello">Hello World, HelloAndroidActivity!</string>  

4.     <string name="app_name">HelloAndroid</string>  

5. </resources>  


    根元素是resources,说明这也是一种资源,而子元素string说明是字符串类型的资源,这里有两个string类型的值,一个是app_name,一个是hello,我们可以在项目的其它地方使用它们。 
    这里补充说明几点内容: 

   为了今后的扩展,我们可以在res/values下创建其它文件,比如存放数组的arrays.xml,存放样式的styles.xml等。 
   同时我们可以在res下创建raw文件夹用于放置多媒体原生文件,如视频,音频等。创建xml文件夹用于放置配置信息。创建anim文件夹用于放置动画效果。
 
    再来看main.xml,就是布局管理器配置文件: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="fill_parent"  

5.     android:orientation="vertical" >  

6.     <TextView  

7.         android:layout_width="fill_parent"  

8.         android:layout_height="wrap_content"  

9.         android:text="@string/hello" />  

10.</LinearLayout>  


    先不深究布局管理器,这里面是我们第一个程序的界面中的内容。一个线性布局管理器中仅有一个组件,就是TextView,用于显示文本信息,这里的android:text属性就是使用的strings.xml中的hello字符串“Hello World,HelloAndroidActivity!”。 
    下面来看一下AndroidManifest.xml,这是我们一个Android项目级的配置文件,是Android项目中最重要的组成部分。比如,现在这个文件中有如下信息: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  

3.     package="org.ourpioneer"  

4.     android:versionCode="1"  

5.     android:versionName="1.0" >  

6.     <uses-sdk android:minSdkVersion="10" />  

7.     <application  

8.         android:icon="@drawable/ic_launcher"  

9.         android:label="@string/app_name" >  

10.        <activity  

11.            android:name=".HelloAndroidActivity"  

12.            android:label="@string/app_name" >  

13.            <intent-filter>  

14.                <action android:name="android.intent.action.MAIN" />  

15.                <category android:name="android.intent.category.LAUNCHER" />  

16.            </intent-filter>  

17.        </activity>  

18.    </application>  

19.</manifest>  


    这里根元素是manifest,schema声明也没什么多说的,其中的package是我们打包的信息,versionCode和versionName表示我们程序的版本。uses-sdk元素的minSdkVsersion表示的是最小版本,2.3.3就是10。 
    <application>元素icon表示我们程序安装在Android设备上的图标,也就是drawable文件夹下的ic_launcher.png。label表示在Android设备中程序的显示名称,就是前面strings.xml中的app_name字符串。 
    <activity>元素是程序中的Activity声明,其中的name连同根元素manifest中的package共同组成Activity的类全名,就是org.ourpioneer.HelloAndroidActivity。子元素<intent-filter>这里先不做说明。 
    图片文件夹中的内容就先不看了,下面看看R.java,看看此时项目为我们自动生成了哪些信息: 

Java代码  

1. /* AUTO-GENERATED FILE.  DO NOT MODIFY. 

2.  * 

3.  * This class was automatically generated by the 

4.  * aapt tool from the resource data it found.  It 

5.  * should not be modified by hand. 

6.  */  

7. package org.ourpioneer;  

8. public final class R {  

9.     public static final class attr {  

10.    }  

11.    public static final class drawable {  

12.        public static final int ic_launcher=0x7f020000;  

13.    }  

14.    public static final class layout {  

15.        public static final int main=0x7f030000;  

16.    }  

17.    public static final class string {  

18.        public static final int app_name=0x7f040001;  

19.        public static final int hello=0x7f040000;  

20.    }  

21.}  


    注释中声明,这是Android生成的文件,不允许修改。final类型的类,是不允许被继承的,在R.java中声明的静态内部类就是我们上面说的各种资源,比如attr,drawable,layout,string等,那么我们在程序中就可以通过R.string.hello来获取到hello。 
     最后来看一下我们仅有的一个Java类,也就是HelloAndroidActivity,这是一个Activity程序,简单来说就是我们和Android程序进行交互的接口,通过Activity程序,我们可以完成界面的显示和事件的处理。 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. public class HelloAndroidActivity extends Activity {  

5.     @Override  

6.     public void onCreate(Bundle savedInstanceState) {  

7.         super.onCreate(savedInstanceState);//Android生命周期方法  

8.         setContentView(R.layout.main);//设置使用的布局管理器  

9.     }  

10.}  


    HelloAndroidActivity类继承了android.app.Activity类,那么就是Android中的一个人机交互程序,覆盖onCreate方法,说明在程序创建时要做的事情,调用父类中方法,也就是可以看作为Android程序的生命周期方法。之后设置使用的布局管理器,调用方式就是通过R.java来进行的。这里R.layout.main就是调用我们layout目录下的main.xml中编写的布局管理器和组件。 
    至此,我们解释了第一个应用程序。之前我们已经在模拟起中看到了这个程序,但是还没有在真正的Android设备中来运行。那么要在Android设备中安装,就要先进行apk文件的创建。我们在项目的bin目录下可以获得为我们自动创建的apk文件,但是我们还是通过手动的方式来看看如何进行apk文件的打包。 
    首先在AndroidManifest.xml中添加下面的内容: 

Xml代码  

1. <uses-permission android:name="android.permission.INSTALL_PACKAGES"/>  


    也就是说我们要有安装包的权限。 
    之后在Eclipe中的项目上点击邮件,选择Export导出,看到如下内容: 
 
    我们选择Export Android Application继续, 
 
    这里是选择我们要导出的Aandroid项目,现在,我们只有一个HelloAndroid,那么就选择继续, 
 
    首次导出,要创建keystore文件,我们选择一个keystore文件的位置,并输入密码继续, 
 
    填写这么多内容就足够了,密码是我们上个页面设置的,然后继续, 
 
    选择apk文件的位置,点击完成,我们就得到了导出的apk安装文件。 
    下面我们在Android设备中来进行实际安装。这里的运行环境为Motorola Defy+,Android版本为2.3.7的MIUI,我们最终得到如下的运行效果,和在模拟器中的运行效果是一致的,至于UI显示略有不同,这就取决于Android设备使用的ROM了: 
 

 

 

 

3.Android学习笔记三:第一个应用程序的扩充

 

 

 之前我们看到的第一个应用程序都是系统为我们自动创建的,那么现在来看看如何对这个程序进行一些简单的扩充。 
    从之前的说明中可以看到,Activity程序完成界面的显示,那么就一定会使用到布局文件,也就是说Activity程序和布局相关的配置联系非常紧密。 
    我们可以在Activity中通过R.java来获取组件,也可以在Activity中动态生成组件。如果要获取组件,那么就需要为组件配置ID,下面我们对main.xml进行简单的扩充,添加几个组件。 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="fill_parent"  

5.     android:orientation="vertical" >  

6.     <TextView  

7.         android:id="@+id/text1"  

8.         android:layout_width="wrap_content"  

9.         android:layout_height="wrap_content"  

10.        android:text="@string/hello" />  

11.    <TextView  

12.        android:id="@+id/text2"  

13.        android:layout_width="wrap_content"  

14.        android:layout_height="wrap_content"  

15.        android:text="@string/powered_by" />  

16.    <Button  

17.        android:id="@+id/btn"  

18.        android:layout_width="fill_parent"  

19.        android:layout_height="wrap_content"  

20.        android:text="@string/btn_text" />  

21.</LinearLayout>  


    我们仍然使用线性布局管理器,schema声明不做说明,layout_width就是布局宽度,这里的fill_parent表示填充整个屏幕,也就是屏幕宽度。同理,layout_height表示布局高度,这里也就是屏幕高度。orientation表示组件的排列形式,也就是水平和竖直排列,这里使用的竖直纵向排列。 
    在该布局管理器中,添加了三个组件,两个TextView和一个Button,首先我们需要为每个组件添加ID,这样便于在程序中进行控制,要注意的是ID的命名格式,必须是@+id/开头,斜杠后面跟上这个组件的ID值即可。每个组件都有layout_width,这里使用wrap_content意思是组件的包裹长度,也就是不会充满屏幕宽度。同理layout_height使用了包裹组件的高度,也不会充满整个屏幕。 
    这里我们加了几个字符串值,strings.xml修改如下: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <resources>  

3.     <string name="hello">Hello World, HelloAndroidActivity!</string>  

4.     <string name="app_name">HelloAndroid</string>  

5.     <string name="powered_by">Powered By Nan Lei</string>  

6.     <string name="btn_text">I\'m Button</string>  

7. </resources>  


    这里要多说一点的就是对'进行转义输出,需要加反斜线。之后运行程序,在模拟器中我们可以看到: 
 
    我们就看到了三个组件的实际运行效果,如果想看横屏显示效果时,在模拟器中可以同时按下Ctrl和F11键即可,显示效果如下: 
 
    再次按下Ctrl和F11即可切回竖屏,可能某些模拟器中从横屏切回竖屏时显示效果回不来,比如: 
 
    此时,按下Esc退出到应用程序列表,选择我们的程序,重新进入即可: 
 
    下面,我们将Button的layout_height调整为fill_parent,来看看会得到怎样的效果: 
 
    那么,也就是说,使用了fill_parent,组件会自动填充整个屏幕,也就是将屏幕下方剩余的区域全部填充满。 
    有了这些认识之后,我们回到R.java中来看看程序为我们自动生成了哪些资源: 

Java代码  

1. package org.ourpioneer;  

2. public final class R {  

3.     public static final class attr {  

4.     }  

5.     public static final class drawable {  

6.         public static final int ic_launcher=0x7f020000;  

7.     }  

8.     public static final class id {  

9.         public static final int btn=0x7f050002;  

10.        public static final int text1=0x7f050000;  

11.        public static final int text2=0x7f050001;  

12.    }  

13.    public static final class layout {  

14.        public static final int main=0x7f030000;  

15.    }  

16.    public static final class string {  

17.        public static final int app_name=0x7f040001;  

18.        public static final int btn_text=0x7f040003;  

19.        public static final int hello=0x7f040000;  

20.        public static final int powered_by=0x7f040002;  

21.    }  

22.}  


    可以看到,我们添加的string资源都已经反映到了R.java的静态类string中,可以通过R.string.hello等即可调用我们设置的字符串资源。而三个组件的ID,也反映到了静态类id中。那么,我们就可以通过Activity程序对它们进行控制了。修改Activity程序如下: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.widget.Button;  

5. import android.widget.TextView;  

6. public class HelloAndroidActivity extends Activity {  

7.     @Override  

8.     public void onCreate(Bundle savedInstanceState) {  

9.         super.onCreate(savedInstanceState);// Android生命周期方法  

10.        super.setContentView(R.layout.main);// 设置使用的布局管理器  

11.        TextView text1 = (TextView) super.findViewById(R.id.text1);//取得text1组件  

12.        text1.setText("你好");//设置text1的显示文字  

13.        Button btn=(Button)super.findViewById(R.id.btn);//取得btn组件  

14.        btn.setText(R.string.btn_text_zh);//设置btn的显示文字  

15.    }  

16.}  


    生命周期方法和设置布局管理器,就不多说了,这里强调的就是必须先设置一下布局管理器,之后我们才能获得其中配置的组件,如果setContentView()放在了最后一行,那么获取组件的操作都会失败。 
    我们使用父类Activity的findViewById()方法可以获取到我们以后的组件,该方法的返回值是View类型,也就是所有视图组件的父类,比如这里通过R.id.text1获取第一个TextView组件,那么进行强制类型转换即可拿到。 
    我们对text1进行了显示文字的设置,这里我们设置为“你好”,就会覆盖掉之前的“Hello World,HelloAndroidActivity!”。之后我们再获取Button组件,原理同上,拿到后我们也修改显示文字,不过这里不是直接使用字符串赋值,而是通过R.string.btn_text_zh的方式进行,也就是通过R.java去配置文件中找到替换的文本,那么我们在strings.xml里再加一行: 

Xml代码  

1. <string name="btn_text_zh">我是按钮</string>  


    下面来运行程序,我们可以看到如下效果: 
 
    现在我们实现了在Activity程序中对已有的布局管理器中的组件进行修改。那么使用Activity程序来生成组件也是可以的,也就是说,我们不在布局管理器文件中进行组件的设置,而是通过编写Java代码来完成。我们不使用main.xml了,之后修改Activity程序: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.widget.Button;  

5. import android.widget.LinearLayout;  

6. import android.widget.TextView;  

7. public class HelloAndroidActivity extends Activity {  

8.     @Override  

9.     public void onCreate(Bundle savedInstanceState) {  

10.        super.onCreate(savedInstanceState);// Android生命周期方法  

11.        LinearLayout layout = new LinearLayout(this);// 创建布局管理器  

12.        layout.setOrientation(LinearLayout.VERTICAL); // 设置组件排列方式  

13.        TextView text1 = new TextView(this);// 创建TextView组件  

14.        text1.setText(super.getString(R.string.hello));// 设置text1的显示文本  

15.        TextView text2 = new TextView(this); // 创建TextView组件  

16.        text2.setText(super.getString(R.string.powered_by));// 设置text2的显示文本  

17.        Button btn = new Button(this);//创建Button组件  

18.        btn.setText(super.getString(R.string.btn_text));// 设置btn的显示文本  

19.        layout.addView(text1);// text1添加到布局管理器中  

20.        layout.addView(text2);// text2添加到布局管理器中  

21.        layout.addView(btn);// btn添加到布局管理器中  

22.        super.setContentView(layout); // 设置布局管理器layout  

23.    }  

24.}  


    结合上面的注释,我们来看一下这段程序,首先我们手动创建了线性布局管理器LinearLayout,所有配置在XML文件中的布局管理器和组件,我们都可以通过代码的形式进行编写。LinearLayout的构造方法接收一个Context类型的参数,这里我们传入的是this,说明我们的Activity类就是Context的子类,看一下类关系图: 
 
java.lang.Object 
  ↳ android.content.Context 
  ↳ android.content.ContextWrapper 
  ↳ android.view.ContextThemeWrapper 
  ↳ android.app.Activity
 
    从图中我们看到android.app.Activity类是android.context.Context类的子类,而我们自定义的HelloAndroidActivity类又是Activity类的子类,那么我们就可以将this传入LinearLayout的构造方法进行LinearLayout对象的构建。创建好layout之后,我们设置一下组件的排列方式,这和写XML的配置是相同的。 
    下面创建了两个TextView组件,因为TextView的构造方法也接受的是Context,那么我们仍然使用this即可,这样我们就构造出了连个TextView组件。然后我们调用Context类的getString()方法进行显示文字的设置,传入的参数仍然是R.java中的id,和直接传入id的效果是一样的,这里只是介绍一下getString()方法。 
    最后创建了一个Button组件,参考Button的构造方法,也不难理解this的传入,之后进行显示文本的设置就行了。 
    最后我们将创建好的三个组件添加到布局管理器中,就是使用了三次addView()方法,之后我们设置Activity使用的布局管理器,也就是调用setContentView()方法将layout传入,之后就可以运行程序进行测试了。这和我们之前看到的效果是一致的。 
 
    使用Java代码动态生成组件完全可以替代XML配置文件,但现在还没有进行其它属性的设置,代码量就已经非常大了,而且写法都是一致的,没有任何技术含量。所以使用XML文件进行视图组件的配置,可以有效的分离显示逻辑,而程序进行控制逻辑,符合MVC设计模式。 

 

 

 

4. Android学习笔记四:基本视图组件:TextView

 

 

TextView在我们第一个应用程序中已经使用过了,通过第一个程序,我们看到Activity程序和视图组件关系密切,这里我们进行深入介绍。 
    Android中的全部视图组件的父类是android.view.View类,看一下文档: 
 
java.lang.Object 
  ↳ android.view.View

    从文档中我们可以看出TextView类是View类的直接子类,而Button类是View的间接子类,后面我们会详细介绍。 
    下面来看看TextView组件,TextView组件主要用于静态文本的显示,这在第一个程序中,我们很容易就能看出来。android.widget.TextView类是android.view.View类的直接子类,这里注意TextView类并不在android.view包中,而是在android.widget包中,也就是说,我们在Aandroid中使用的具体视图组件,都是在android.widget包中的。下面看一下TextView类的文档: 
 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.widget.TextView

    从中我们看到了类的继承关系,也知道了Button类和EditText类是TextView的直接子类,并从类说明中了解到TextView用于文本的显示,而要修改文本的内容,我们就要使用EditText了。 
    了解过基本内容后,我们来深入了解一下TextView组件。 
    在Eclipse中我们新建一个项目TextViewDemo, 
 
    之后我们来编写几个基本的TextView组件: 

Xml代码  

1. <TextView  

2.     android:id="@+id/text1"  

3.     android:layout_width="wrap_content"  

4.     android:layout_height="wrap_content"  

5.     android:textColor="#FDF5E6"  

6.     android:textSize="16dp"  

7.     android:text="这里是一个TextView视图组件" />  


    在这个TextView中我们较之前的示例添加了两个属性,一个是textColor,很显然这是设置文件颜色的,另外一个是textSize,设置文字大小。 
    这里多说一点,关于文字大小,是数字加单位的格式,这里我们使用的是dp,当然还有pt,px,mm等值可选。 
    运行程序,我们来看看效果: 
 
    可以看到文本的颜色变化了,字体也变化了,说明我们的设置都起作用了。 
    下面再来看第二个TextView示例: 

Xml代码  

1. <TextView  

2.     android:id="@+id/text2"  

3.     android:layout_width="wrap_content"  

4.     android:layout_height="wrap_content"  

5.     android:layout_margin="30dp"  

6.     android:autoLink="all"  

7.     android:text="Google一下: http://www.google.com.hk" />  


    这里的文本中我包含的一个超链接,这样我们就可以通过点击超链接来访问网站,如果在TextView中想让文本中的超链接自动识别成地址,那么需要加入autoLink属性,其值有多种,这里设置成all表示所有的可链接的地址都识别,包括email地址。还有一个属性是layout_margin,这是样式属性,表示页边距,它和CSS中的margin属性类似,这里表示距离上下左右各30dp的距离,其余属性不再说明,下面看一下运行效果: 
 
    可以看出,首先样式上,距离上下左右各有30dp的间隔,其次是其中的网址信息已经被识别成了超链接,那么我们就可以点击这个网址来访问想要访问的站点,保证模拟器运行环境是可以上网的,那么点击该地址,我们可以看到: 
 
    就访问到了移动版的Google(这里切到横屏显示,效果更好一点)。 
    下面继续对TextView进行说明,看第三个示例: 

Xml代码  

1. <TextView  

2.         android:id="@+id/text3"  

3.         android:layout_width="wrap_content"  

4.         android:layout_height="wrap_content"  

5.         android:layout_marginLeft="40dp"  

6.         android:maxLength="7"  

7.         android:text="@string/powered_by" />  


    这里设置的属性有layout_marginLeft,从字母理解就是左边距,也是样式上的设置,和CSS中的margin-left也是一样的,那么这个组件仅仅是对左边有40dp的距离,而上下和右侧则不会有边距了。默认的显示文本是“Powered By Nan Lei”,但这里我们多设置了一个属性是maxLength,也就是最大显示的长度,是7个字符长度,也就是说,我们只会看到“Powered”显示出来了,后面的文本就被截掉了,看一下运行效果: 
 
    下面来看第四种TextView的示例,我们将会插入一张图片: 

Xml代码  

1. <TextView  

2.     android:id="@+id/text4"  

3.     android:layout_width="wrap_content"  

4.     android:layout_height="wrap_content"  

5.     android:background="@drawable/ic_launcher"  

6.     android:text="图片上的文字"  

7.     android:textColor="#FFFFFF"  

8.     android:textStyle="italic" />  


    和之前的示例比较,这里我们多用了一个background属性,就是用来表示背景图片的,那么我们使用项目自带的一个图片,就是Android机器人的图标,引用图片资源,要使用@drawable来引用,那么就得到如下的显示效果了: 
 
    此时,文字就在图片上方显示出来了。 
    看了四个示例之后,我们会发现,对显示文本的样式设置较多,结合CSS的使用,我们在网页中常把CSS文件单独分离出来,便于后期的维护。类似地,在Android中,我们也可以将用于样式控制的属性单独抽取出来形成样式文件,而在TextView组件中直接引用样式即可,那么对样式属性的维护就变得容易了,我们来设置显示样式。 
    首先需要在values文件夹下创建styles.xml,用于放置样式的配置信息,内容如下: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <resources>  

3.     <style name="text_style">  

4.         <item name="android:textSize">16dp</item>  

5.         <item name="android:textColor">#FDF5E6</item>  

6.         <item name="android:textStyle">italic|bold</item>  

7.         <item name="android:layout_width">wrap_content</item>  

8.         <item name="android:layout_height">wrap_content</item>  

9.     </style>  

10.</resources>  


    和strings.xml文件中的内容类似,只是这里我们使用了style来定义样式,给出一个样式的名称,这里为text_style,那么就可以在style元素中来设置具体的样式了。每个样式使用item来定义,name就是前面看到的样式名称,然后给出样式属性的值,这和前面都是一样的,只是这里在定义字体时,使用了斜体粗体并存的样式,使用竖线分隔开即可。然后修改我们的TextView定义: 

Xml代码  

1. <TextView  

2.      android:id="@+id/text1"  

3.      style="@style/text_style"  

4.      android:text="这里是一个TextView视图组件" />  


    这里面已经去掉了所有和样式相关的内容,然后使用style属性来引用外部样式表中配置的样式即可,运行程序,得到如下效果: 
 
    可以看到我们的样式对一个TextView组件起作用了,说明我们的代码是好用的。 
    做了这么多的东西,看看我们哪些资源已经反映到了R.java中: 

Java代码  

1. package org.ourpioneer;  

2. public final class R {  

3.     public static final class attr {  

4.     }  

5.     public static final class drawable {  

6.         public static final int ic_launcher=0x7f020000;  

7.     }  

8.     public static final class id {  

9.         public static final int text1=0x7f060000;  

10.        public static final int text2=0x7f060001;  

11.        public static final int text3=0x7f060002;  

12.        public static final int text4=0x7f060003;  

13.    }  

14.    public static final class layout {  

15.        public static final int main=0x7f030000;  

16.    }  

17.    public static final class string {  

18.        public static final int app_name=0x7f040001;  

19.        public static final int hello=0x7f040000;  

20.        public static final int powered_by=0x7f040002;  

21.    }  

22.    public static final class style {  

23.        public static final int text_style=0x7f050000;  

24.    }  

25.}  


    我们没有设置attr资源,所以该类下是空的,id资源表示了我们配置的四个TextView组件,layout只有一个main,string资源有三个,这是我们配置的,最后一个是style资源,这里就是我们配置的text_style样式表。 
    前面说过所有的XML配置都可以通过代码来实现,那么我们看看对text3在程序中进行进一步的设置: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.widget.TextView;  

5. public class TextViewDemoActivity extends Activity {  

6.     @Override  

7.     public void onCreate(Bundle savedInstanceState) {  

8.         super.onCreate(savedInstanceState);  

9.         super.setContentView(R.layout.main);  

10.        TextView text3 = (TextView) super.findViewById(R.id.text3);  

11.        text3.setTextAppearance(this, R.style.text_style);  

12.    }  

13.}  


    这段代码不用过多解释了,获取到text3组件之后,设置显示的样式,只是注意这里不是setStyle()方法,而是setTextAppearance()方法。该方法参数为Context和resId,那么只需传入this和R.style.text_style即可。我们来看下对text3组件的效果: 
 
    我们就可以看到最后的效果了,text3组件的显示效果也变化了,说明我们的代码起作用了。 

 

 

 

 

 

 

 

 

 

 

 

 

5.Android学习笔记五:基本视图组件:Button

 

 

  Button组件也是我们前面看到过的一个简单组件,这里我们来进行深入的介绍。按钮的基本功能就是供用户点击,然后产生一些效果,比如Web开发中的登录按钮,点击之后即可实现登录效果。 
    这里我们没有对Button的事件处理操作,还是简单的了解Button的配置。首先来看一下Button的文档: 
 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.widget.TextView 
  ↳ android.widget.Button
 
   可以看到Button类是TextView类的子类,而不是直接继承自View类的。可以说Button是一个特殊的TextView,下面在Eclipse中新建一个项目来演示Button组件: 

Xml代码  

1. <Button  

2.     android:id="@+id/btn1"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:text="这是一个Button组件"  

6.     android:textColor="#FDF5E6"  

7.     android:textSize="16dp" />  


    可以看出,Button组件的配置和TextView是极其类似的,因为从类的继承结构上就不难得出这个结论。这里我们也是设置了按钮组件显示的文本,文本的颜色以及文本的大小,下面运行程序来看一下具体效果: 
 
    再来看第二个Button示例: 

Xml代码  

1. <Button  

2.     android:id="@+id/btn2"  

3.     android:layout_width="wrap_content"  

4.     android:layout_height="wrap_content"  

5.     android:layout_gravity="center"  

6.     android:layout_marginTop="20dp"  

7.     android:text="点击访问: http://www.google.com.hk"  

8.     android:textSize="10dp" />  


    这个Button组件中,我们设置layout_width为wrap_content,也就是包裹文字大小,而不是充满屏幕。同时设置了layout_gravity属性为center,也就是居中显示,而layout_marginTop的意思就是距离上个组件的上边距为20dp,这和CSS中的概念也是一致的。重新设置Button的显示文本和文字大小,再次运行程序,这次在横屏下显示,来看一下效果: 
 
    可以看到最终的效果就是上边距为20dp,而左右边距没有变化,仅仅是包裹文字的宽度来显示的。 
    下面再来看一个示例: 

Xml代码  

1.     <Button  

2.         andr  

3. oid:id="@+id/btn3"  

4.         android:layout_width="fill_parent"  

5.         android:layout_height="wrap_content"  

6.         android:layout_gravity="center"  

7.         android:layout_margin="15dp"  

8.         android:maxLength="14"  

9.         android:text="Powered By Nan Lei"  

10.        android:textSize="12dp" />  


    这里我们调整了layout_width为fill_parent,而设置layout_margin为15dp,也就是上下左右边距都为15dp,再设置最大的显示长度为14个字符,之后运行程序,我们可以看到如下的显示效果: 
 
    要注意的是这里如果设置layout_width为wrap_content,那么就不会得到左右边距15dp的效果,而仅仅是包裹文字了。具体所需的显示效果,我们可以根据具体需求去详细调整。 
   从上面三个例子中可以很容易看出,Button组件的操作和TextView是基本一致的。最后我们来看看在程序中动态生成按钮,代码如下: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.widget.Button;  

5. import android.widget.LinearLayout;  

6. public class ButtonDemoActivity extends Activity {  

7.     @Override  

8.     public void onCreate(Bundle savedInstanceState) {  

9.         super.onCreate(savedInstanceState);  

10.        super.setContentView(R.layout.main);  

11.        LinearLayout layout = (LinearLayout) super.findViewById(R.id.layout);  

12.        Button btn = new Button(this);  

13.        btn.setText("我是在程序中动态创建的按钮");  

14.        layout.addView(btn);  

15.    }  

16.}  


    程序代码很简单,但是要说一点就是,我们通过findViewById()方法获取布局管理器时,要在XML中为该布局管理器设置id,也就是说,在main.xml中,我们要修改代码: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:id="@+id/layout"  

4.     android:layout_width="fill_parent"  

5.     android:layout_height="fill_parent"  

6.     android:orientation="vertical" >  

7. ......  

8. </LinearLayout>  


   这样,上面的程序代码就可以执行了,效果如下: 
 
    至此,我们已经看到了最后的显示效果,我们把一个Button动态的添加到了布局管理器中,此时来看看R.java中都有什么: 

Java代码  

1. package org.ourpioneer;  

2. public final class R {  

3.     public static final class attr {  

4.     }  

5.     public static final class drawable {  

6.         public static final int ic_launcher=0x7f020000;  

7.     }  

8.     public static final class id {  

9.         public static final int btn1=0x7f050001;  

10.        public static final int btn2=0x7f050002;  

11.        public static final int btn3=0x7f050003;  

12.        public static final int layout=0x7f050000;  

13.    }  

14.    public static final class layout {  

15.        public static final int main=0x7f030000;  

16.    }  

17.    public static final class string {  

18.        public static final int app_name=0x7f040001;  

19.        public static final int hello=0x7f040000;  

20.    }  

21.}  


    可以看到我们为布局管理器加的ID也显示出来了。 
    最后,我们将该程序安装到Android设备上来具体看看效果(运行设备为Motorola Defy+ Android 2.3.7 MIUI):
 

 

 

6. Android学习笔记六:基本视图组件:EditText

 

 

类似于Web开发中的HTML输入框,EditText就是Android中的文本输入框。前面介绍的TextView仅仅用于静态文本的显示,而需要用户进行输入的话,就需要使用EditText组件。 
    首先,看一下EditText的文档: 
 

java.lang.Object 
  ↳ android.view.View 
  ↳ android.widget.TextView 
  ↳ android.widget.EditText 

    和Button组件一样,EditText也是TextView的子类,可以看作是TextView类的扩展组件。那么我们开始编写EditText组件的代码: 

Xml代码  

1. <EditText  

2.     android:id="@+id/edit1"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:text="@string/hello" />  


    这是最基本的EditText组件的展示,我们可以获得如下效果: 
 
   效果就是出现一个文本框,里面出现我们给出的默认文字。下面修改这个组件代码: 

Xml代码  

1. <EditText  

2.     android:id="@+id/edit1"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:selectAllOnFocus="true"  

6.     android:text="@string/hello" />  


    我们添加了一个属性,叫selectAllOnFocus,那么值为true,也就是默认全部选中,至于显示的效果,运行代码我们就可以看到: 
 
    不同之处就是EditText中的文本在初始状态时已经被默认全部选中了。 
    我们继续来看EditText组件的配置: 

Xml代码  

1. <EditText  

2.     android:id="@+id/edit2"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:enabled="false"  

6.     android:text="http://www.google.com.hk" />  


    这里我们加了一个属性是enabled,值为false,不难从字面理解就是不可以使用,那么运行效果如下: 
 
    在Web开发中,HTML文本输入框还有一种密码输入框,这样的设置是必要的,那么在Android中,我们也有对应的属性来设置: 

Xml代码  

1. <EditText  

2.     android:id="@+id/edit3"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:inputType="textPassword"  

6.     android:text="password" />  


    这里我们加入了inputType属性,值设置为textPasswrod,那么也就是密码显示效果,文本就不会以明文效果显示出来了,而是使用原点来作为替代。这样的设置是比较新的写法,我们还可以使用来表示android:password="true"密码输入框。 
    运行程序,我们得到如下的显示效果: 
 
    现在我们已经在Android中实现了对HTML文本输入框和密码输入框的实现。在Web开发中我们有一种需求是对输入文本的限制,比如年龄输入框限制为必须是数字,而不能混合输入数字和字母等,这需要编写JavaScript代码来实现限制或是校验,但是在Android中我们可以直接做到限制效果,代码如下: 

Xml代码  

1. <EditText  

2.     android:id="@+id/edit4"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:inputType="number"  

6.     android:text="24abc" />  


    同样,还有一种老式写法为android:numeric="integer",表达的都是同样的意义,这里的取值可以有number,decimal等,具体可以参考文档去查找,或者在Eclipse中代码的提示功能也会看到。这里的默认文本我们输入24abc,带有字母,它可以正常的显示出来,但是在运行中,该输入框是不接受字母输入的,下面运行程序,我们可以看到: 
 
    将鼠标点击在该EditText组件上,使用键盘输入,发现字母无法输入,而数字可以被接受,就是这样的效果。 
    同样,我们可以在Activity程序中来进行组件的动态生成,我们编写如下代码: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.text.InputType;  

5. import android.widget.EditText;  

6. import android.widget.LinearLayout;  

7. public class EditTextDemoActivity extends Activity {  

8.     public void onCreate(Bundle savedInstanceState) {  

9.         super.onCreate(savedInstanceState);  

10.        super.setContentView(R.layout.main);  

11.        LinearLayout layout = (LinearLayout) super.findViewById(R.id.layout);  

12.        EditText edit = new EditText(this);  

13.        edit.setInputType(InputType.TYPE_CLASS_NUMBER);  

14.        edit.setText("2.4");  

15.        layout.addView(edit);  

16.    }  

17.}  


   操作步骤和之前的类似,先获取到布局管理器,之后创建一个EditText组件,然后设置inputType为数字,之后设置默认显示文本为2.4,最后将该组件加入到布局管理器之中。 
    运行程序,我们就得到了最后的效果: 
 
    同样,这里是无法接受字母输入的。 

 

 

 

 

 

 

 

 

7. Android学习笔记七:基本视图组件:RadioGroup和RadioButton

 

 

 

在Web开发中,HTML的<input type="radio">可以实现单选按钮,单选按钮主要用于多值选一的操作,比如性别的选择,仅能从“男”或“女”中选择,那么就可以使用单选按钮实现。那么在Android中实现单选就需要使用到RadioGroup和RadioButton两个视图组件,它们结合使用才能达到单选按钮的效果。 
    首先我们来看看这两个组件的文档: 
 

java.lang.Object 
  ↳ android.view.View 
  ↳ android.view.ViewGroup 
  ↳ android.widget.LinearLayout 
  ↳ android.widget.RadioGroup
 
    可以看出,RadioGroup是线性布局管理器LinearLayout的子类,那么也就说明,在RadioGroup中的组件是线性排列的,也就是说RadioButton线性排列在RadioGroup内。RadioGroup可以视为是RadioButton的容器,下面来看看RadioButton的文档: 
 

java.lang.Object 
  ↳ android.view.View 
  ↳ android.widget.TextView 
  ↳ android.widget.Button 
  ↳ android.widget.CompoundButton 
  ↳ android.widget.RadioButton
 
   RadioButton是Button类的间接子类,它直接继承自CompoundButton,这样的设计也是为了复选框Checkbox的设计提出公共的父类。 
   下面在Eclipse中新建一个项目,我们来介绍RadioGroup和RadioButton的使用: 

Xml代码  

1. <TextView  

2.     android:id="@+id/genderLabel"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:text="请选择您的性别"  

6.     android:textSize="20dp" />  

7. <RadioGroup  

8.     android:id="@+id/gender"  

9.     android:layout_width="fill_parent"  

10.    android:layout_height="wrap_content"  

11.    android:orientation="vertical" >  

12.    <RadioButton  

13.        android:id="@+id/male"  

14.        android:text="" />  

15.    <RadioButton  

16.        android:id="@+id/female"  

17.        android:text="" />  

18.</RadioGroup>  


    对于TextView组件不做解释了,就是用于一个提示信息,之后我们定义了一个RadioGroup,设置这个组件的一些属性,这里我们设置ID和长宽信息。注意它是先行布局管理器的子类,是放置RadioButton的容器,那么这里我们这是一个orientation属性来规定RadioButton的排列方式,这里vertical表示纵垂直向排列。 
    在RadioGroup中定义了两个RadioButton组件,就是两个具体的单选按钮,首先分别设置ID,之后就是要显示的文字,没什么可多说的。下面运行程序来看看效果: 
 
    这里呈现出了单选按钮的效果。在Web开发中,HTML的单选按钮有默认选中设置,使用<input type="radio"checked>来标识,那么在Aandroid中,我们也可以设置默认选中的项目,修改代码如下 

Xml代码  

1. <RadioButton  

2.     android:id="@+id/male"  

3.     android:checked="true"  

4.     android:text="" />  


    这里表示默认选中“男”,和HTML代码十分类似,但我们还有另外一种标识默认选中的方法,代码如下: 

Xml代码  

1. <RadioGroup  

2.     android:id="@+id/gender"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:checkedButton="@+id/female"  

6.     android:orientation="vertical" >  


    我们在RadioGroup中用checkedButton属性来设置也是可以的,其直为RadioButton的ID,那么我们就不用在具体的RadioButton上来标识了,从而保持RadioButton代码的一致性,运行程序,得到如下效果: 
 
    此时和我们设置的是一致的,默认选中的是“女”。默认选中的意义在于,如果用户漏选某个值,而这个值要提交给后台程序处理,恰恰又没有校验的时候,那么不会引起后台程序的错误,这个设置是很有必要的。在Web开发中,对于单选按钮,通常也会设置默认选中的一个值。 
    看过竖直排列后,我们来看看水平排列的效果,很简单,修改RadioGroup的orientation为horizontal即可: 

Xml代码  

1. <RadioGroup  

2.     android:id="@+id/gender"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:checkedButton="@+id/male"  

6.     android:orientation="horizontal" >  


    运行代码,我们可以看到如下效果: 
 
    这样,选项就水平排列了。 
   下面我们使用程序来对RadioGroup和RadioButton进行控制,比如新加入一个需求,性别可以选择保密,并且设置保密为默认选中状态,那么在代码中,我们可以这么来实现: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.widget.RadioButton;  

5. import android.widget.RadioGroup;  

6. public class RadioDemoActivity extends Activity {  

7.     private RadioGroup radioGroup;  

8.     private RadioButton radio;  

9.     public void onCreate(Bundle savedInstanceState) {  

10.        super.onCreate(savedInstanceState);  

11.        super.setContentView(R.layout.main);  

12.        radio = new RadioButton(this);  

13.        radio.setChecked(true);  

14.        radio.setText("保密");  

15.        radioGroup = (RadioGroup) super.findViewById(R.id.gender);  

16.        radioGroup.addView(radio);  

17.    }  

18.}  


   在这里,我们将RadioGroup和RadioButton作为类的成员变量出现。首先是创建RadioButton组件,和之前的组件一样,构造方法接受一个Context类型的变量,就是this。其次设置这个按钮为默认选中的状态,并且给出提示信息“保密”。之后获取到我们在main.xml文件中定义的RadioGroup组件,并向这个RadioGroup中添加我们新创建的RadioButton,那么运行程序,我们可以看到如下效果: 
 
    这样也就实现了在程序中动态地对单选按钮组件的控制。 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8. Android学习笔记八:基本视图组件:CheckBox

 

 

 

在Web开发中,HTML中有复选框CheckBox设置<input type="checkbox">,复选框用于在一组值中选择多个,比如个人爱好,可以从一组值中选择多个。而在Android中,对于复选框,可以使用CheckBox组件即可实现。 
    首先,我们看一下CheckBox的文档: 
 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.widget.TextView 
  ↳ android.widget.Button 
  ↳ android.widget.CompoundButton 
  ↳ android.widget.CheckBox
 
    我们前面说过了,CheckBox和RadioButton是直接继承自CompoundButton的,表示对复合式Button的一个抽象。下面我们从代码中来看看CheckBox的使用,在Eclipse中创建CheckBox的演示项目,编写代码如下: 

Xml代码  

1. <TextView  

2.     android:id="@+id/favouriteLabel"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:text="请选择您的爱好" />  

6. <CheckBox  

7.     android:id="@+id/swimming"  

8.     android:layout_width="fill_parent"  

9.     android:layout_height="wrap_content"  

10.    android:text="游泳" />  

11.<CheckBox  

12.    android:id="@+id/climbing"  

13.    android:layout_width="fill_parent"  

14.    android:layout_height="wrap_content"  

15.    android:text="登山" />  

16.<CheckBox  

17.    android:id="@+id/shopping"  

18.    android:layout_width="fill_parent"  

19.    android:layout_height="wrap_content"  

20.    android:text="购物" />  


    一个用于提示信息的TextView组件就不多说了,后面跟着三个CheckBox组件,每个组件都设置了ID和长宽信息,最后都有一个显示的文本,那么基本的CheckBox组件就实现出来了,运行程序,我们可以看到如下效果: 
 
    和单选框一样,复选框也可以进行默认选中的配置,下面我们使用程序代码来演示: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.widget.CheckBox;  

5. import android.widget.LinearLayout;  

6. public class CheckBoxDemoActivity extends Activity {  

7.     private LinearLayout layout;  

8.     private CheckBox gaming;  

9.     public void onCreate(Bundle savedInstanceState) {  

10.        super.onCreate(savedInstanceState);  

11.        super.setContentView(R.layout.main);  

12.        layout = (LinearLayout) super.findViewById(R.id.layout);  

13.        gaming = new CheckBox(this);  

14.        gaming.setText("游戏");  

15.        gaming.setChecked(true);  

16.        layout.addView(gaming);  

17.    }  

18.}  


    在编写代码之前,还是要为我们的布局管理器加上ID,以便在程序中进行操作。这里定义了两个成员变量,表示布局管理器和我们要添加的一个CheckBox组件。 
    首先,我们获取到布局管理器对象,使用findViewById()方法,很简单。下面是创建一个新的CheckBox组件,和其它视图组件一样,它的构造方法也是接受一个Context类型的对象,那么就是this。之后对这个CheckBox组件进行设置,显示文本为“游戏”,并且设置默认选中。最后将它加入到布局管理器中就可以了。 
    运行程序,我们可以看到如下效果: 
 
    我们在Activity程序中动态创建的CheckBox也显示出来了,并且已经被默认选中了。 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

9. Android学习笔记九:基本视图组件:Spinner

 

 

 

 在Web开发中,HTML提供了下拉列表的实现,就是使用<select>元素实现一个下拉列表,在其中每个下拉列表项使用<option>表示即可。这是在Web开发中一个必不可少的交互性组件,在Android中的对应实现就是Spinner。
    首先来看一下Spinner的文档: 
 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.view.ViewGroup 
  ↳ android.widget.AdapterView<T extends android.widget.Adapter> 
  ↳ android.widget.AbsSpinner 
  ↳ android.widget.Spinner
 
    Spinner的继承结构比较复杂,在继承树中有AdapterView,这是比较重要的一项,因为我们就是通过Adapter来为Spinner设置下拉列表项的。 
    Spinner的重点问题就是下拉列表项的配置,通过之前组件的了解,我们知道资源组件的配置有两种方式,一种是通过XML文件来配置,一种是通过程序来配置。而从Spinner的文档中,我们可以看到,对它的配置需要使用Adapter类的实现。 
    下面我们就来看看Spinner的使用,在Eclipse中创建SpinnerDemo项目,编写代码: 

Xml代码  

1. <TextView  

2.     android:id="@+id/degreeLabel"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:text="请选择您的学历" />  

6. <Spinner  

7.     android:id="@+id/degree"  

8.     android:layout_width="fill_parent"  

9.     android:layout_height="wrap_content" />  


    这里放置了一个空的Spinner,直接运行程序,我们看到如下效果: 
 
    就是一个空的下拉框,什么都没有,下面我们开始向这个下拉框中设置列表项。首先我们使用XML资源文件的配置方式,在values目录下创建spinner_data.xml,并设置如下内容: 

Xml代码  

1. <string-array name="degrees">  

2.     <item>初中及以下</item>  

3.     <item>高中</item>  

4.     <item>大学</item>  

5.     <item>研究生及以上</item>  

6. </string-array>  


    不难理解<string-array>表示字符串的数组,就是可以定义多个字符串的项目,在其中,我们仍然使用<item>来定义每一项,设置好后,将其应用到Spinner中: 

Xml代码  

1. <Spinner  

2.     android:id="@+id/degree"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:entries="@array/degrees" />  


    配置Spinner组件中的entries属性即可引用我们在spinner_data.xml中定义的degrees项目组了,不难想到,它已经被注册到R.java中了。运行程序,我们得到如下显示效果: 
 
    这里可以看到,我们虽然设置了提示信息,但是点击下拉框后并没有出现Spinner的提示,而是直接显示出了我们设置的选项,这样的显示并不是很好看,我们希望在弹出的Spinner选择框上也给出提示,那么我们可以这么来设置: 

Xml代码  

1. <Spinner  

2.     android:id="@+id/degree"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:entries="@array/degrees"  

6.     android:prompt="@string/degrees_prompt" />  


    也就是在Spinner组件中加上prompt属性即可,注意这里不能直接使用文本了,而需要使用引用,那么我们在strings.xml中加入如下信息: 

Xml代码  

1. <string name="degrees_prompt">您的学历是:</string>  


   我们再次运行程序,就可以看到如下的效果: 
 
    这次再点击Spinner,弹出的下拉框中就有了提示的prompt,显示效果好了很多。 
    下面我们看看如何通过程序来控制Spinner和列表项,之前说过要使用程序,就要用到Adapter类,这里我们使用实现类ArrayAdapter来进行操作。首先我们使用ArrayAdapter读取XML配置文件的方式来说明: 

Xml代码  

1. <string-array name="cities">  

2.     <item>北京</item>  

3.     <item>上海</item>  

4.     <item>大连</item>  

5. </string-array>  


    我们在spinner_data.xml中再设置一组值来表示城市,有了列表项,我们还需要再创建一个Spinner,那么在main.xml中,再设置一个Spinner: 

Xml代码  

1. <TextView  

2.     android:id="@+id/cityLabel"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:text="请选择您所在的城市" />  

6. <Spinner  

7.     android:id="@+id/city"  

8.     android:layout_width="fill_parent"  

9.     android:layout_height="wrap_content" />  


    那么在Activity程序中,我们可以如下来编写代码: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.widget.ArrayAdapter;  

5. import android.widget.Spinner;  

6. public class SpinnerDemoActivity extends Activity {  

7.     private Spinner city = null;// 要读取的下拉列表  

8.     private ArrayAdapter<CharSequence> cities = null;// 要使用的Adapter  

9.     public void onCreate(Bundle savedInstanceState) {  

10.        super.onCreate(savedInstanceState);  

11.        super.setContentView(R.layout.main);  

12.        city = (Spinner) super.findViewById(R.id.city);// 获取下拉列表  

13.        city.setPrompt("您所在的城市是:");// 设置Prompt  

14.        cities = ArrayAdapter.createFromResource(this, R.array.cities,android.R.layout.simple_spinner_item);// 实例化ArrayAdapter  

15.        city.setAdapter(cities);// 设置显示信息  

16.    }  

17.}  


    根据代码中的注释,不难理解每行代码的意思,这和在XML中配置Spinner是类似的。要多说的一点就是这里我们为ArrayAdapter设置的泛型是CharSequence,而不是直接使用String,这是出于以后对StringBuffer的兼容,来看下CharSequence的文档: 
 
    不难看出,String和StringBuffer都是CharSequence的子类,这里定义为CharSequence后直接使用字符串格式是没有问题的。 
    下面直接运行程序,我们可以看到如下效果: 
 
    此时列表项的显示和之前的不同,是因为我们在实例化ArrayAdapter时使用了android.R.layout.simple_spinner_item,也就是使用了简单的spinner项。 
    可以通过如下的代码控制显示风格: 

Java代码  

1. cities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 设置列表项的显示风格  


    之后再次运行代码,就可以看到和之前一样的显示效果了。 
    下面来看第二种ArrayAdapter的使用方式,就是在程序中动态生成结果来进行填充,我们修改上面的代码,最终为: 

Java代码  

1. package org.ourpioneer;  

2. import java.util.Arrays;  

3. import java.util.List;  

4. import android.app.Activity;  

5. import android.os.Bundle;  

6. import android.widget.ArrayAdapter;  

7. import android.widget.LinearLayout;  

8. import android.widget.Spinner;  

9. import android.widget.TextView;  

10.  

11.public class SpinnerDemoActivity extends Activity {  

12.    // 省略City部分SpinnerArrayAdapter声明的代码  

13.    private Spinner age = null;// 声明一个Spinner组件,表示年龄  

14.    private ArrayAdapter<CharSequence> ages = null;// 声明一个ArrayAdapter来适配年龄  

15.    private List<CharSequence> age_data = null;// 声明一个放置年龄数据的List  

16.    public void onCreate(Bundle savedInstanceState) {  

17.        super.onCreate(savedInstanceState);  

18.        super.setContentView(R.layout.main);  

19.        // 省略City部分的设置代码  

20.        age = new Spinner(this);// 创建Spinner对象  

21.        age.setPrompt("您的年龄段是:");// 设置Prompt  

22.        age_data = Arrays.asList(new CharSequence[] { "10岁以下""10-20",  

23.                "20-30""30-40""40-50""50-60""60岁以上" });// 设置年龄段数组并最终转换为List类型  

24.        ages = new ArrayAdapter<CharSequence>(this,  

25.                android.R.layout.simple_spinner_item, age_data);// 实例化ArrayAdapter  

26.    ages.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 设置列表项显示风格  

27.        age.setAdapter(ages);// 设置显示信息  

28.        LinearLayout layout = (LinearLayout) super.findViewById(R.id.layout);  

29.        TextView ageLabel=new TextView(this);  

30.        ageLabel.setText("请选择您的年龄段");  

31.        layout.addView(ageLabel);  

32.        layout.addView(age);  

33.    }  

34.}  


    在此之前,不要忘了在main.xml中为我们的布局管理器加上ID属性,这样才能在程序中进行调用,运行程序,我们可以看到如下效果: 
 
    点击即可弹出下拉列表框显示具体内容了。 
    下面我们在Android设备上来运行Spinner的示例程序,运行环境为Motorola Defy+ 2.3.7 MIUI,所得到的具体显示效果为: 
 
    可以看出,这和我们在模拟器中的显示效果略有不同,那么就是手机所刷ROM对我们组件显示风格的影响了,也就是说,在不同的ROM下,会有不同的显示效果。 

10. Android学习笔记十:基本视图组件:ImageView和ImageButton

 

 

 

在Web开发中,HTML中对图片的操作就是提供一个<img>标签,我们通过该标签的src属性来制定图片资源的地址,从而在页面中显示一个图片。那么在Android中,ImageView就是用于图片显示的。 
    首先,我们来看一下ImageView的文档: 
 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.widget.ImageView
 
    可以看出,ImageView的继承结构并不复杂,它是View类的直接子类。下面就来看看关于ImageView的介绍,首先在Eclipse中重新创建一个项目,编写如下代码: 

Xml代码  

1. <ImageView  

2.     android:id="@+id/img"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:contentDescription="ImageView Demo"  

6.     android:src="@drawable/ic_launcher" />  


    代码非常的简单,ID就不多说了,然后设置长和宽,contentDescription是对图片的一个简要说明,最重要的src属性,这和HTML中的属性名称是一致的,也就是指定图片资源的位置。我们知道,Android项目中的图片资源统一位于res/drawable-xx文件夹内,不同的分辨率用于不同的设备,而我们只需通过@drawable/来调用图片资源即可。放置在drawable文件夹内的图片,会在R.java中自动注册,所以我们才能访问到其中的图片资源。 
    这个程序很简单,我们直接运行代码看一下效果: 
 
    对于ImageView没有什么可以多说的内容,下面直接来看看ImageButton。 
    ImageButton就是图片按钮,在HTML中,我们定义按钮可以通过<button>标签来定义,表单中的按钮还可以是<submit>和<reset>,但是原生的HTML对按钮没有任何装饰,显示效果不好,我们可以通过CSS来美化按钮,比如加上图片。而在Android中,有ImageButton组件,可以为我们直接达到此目的。 
    首先,来看一下ImageButton的文档: 
 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.widget.ImageView 
  ↳ android.widget.ImageButton
 
    应该注意到了,ImageButton是和Button没有任何关系的。Button类是TextView类的子类,而ImageButton类却是ImageView的子类。图片按钮,肯定需要图片,之前我们介绍过,Android的所有图片资源都在res/drawable-xx文件夹内,那么我们直接来看代码,把上面的ImageView的代码稍微修改一下: 

Xml代码  

1. <ImageButton  

2.     android:id="@+id/img"  

3.     android:layout_width="wrap_content"  

4.     android:layout_height="wrap_content"  

5.     android:contentDescription="ImageButton Demo"  

6.     android:src="@drawable/ic_launcher" />  


    只是修改了一下标签名称,其余内容都不需要变化,那么再次运行程序,我们可以看到如下效果: 
 
   很容易看出,就是在按钮上加了图片的效果,只是这里我们的图片比较小,按钮部分还留有显示,只要做出合适大小的按钮图片,那么ImageButton就全是图片显示了,只不过这个图片可以进行点击操作。关于ImageButton也没有需要多说的了,需要时会用就行了。 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

11. Android学习笔记11:线性布局管理器LinearLayout

 

 

 

和Java GUI部分的概念类似,布局管理器用于界面的布局操作,并装载视图组件。在前面的程序中,我们最先涉及到的就是线性布局管理器,对它也有了一定的了解。 
    首先,我们来看看线性布局管理器的文档: 
 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.view.ViewGroup 
  ↳ android.widget.LinearLayout
 
    因为布局管理器也是视图组件,所以都继承自View类,之前的所有示例程序都是使用的线性布局管理器了,对它应该是最熟悉的。下面新建一个项目来看: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="fill_parent"  

5.     android:orientation="horizontal" >  

6. </LinearLayout>  


    每个新建的默认项目都会创建一个main.xml文件,其中就是一个线性布局管理器。这里面的xmlns是XML文档的命名空间,这里就是android,那么以android:开头的属性都是该命名空间下的。首先来看layout_width,设置布局管理器的宽度,取值有match_parent,fill_parent和wrap_content,对于布局管理器来说,我们一般使用fill_parent来表示填充整个屏幕宽度。同理,layout_height表示的是布局管理器的高度,我们同样使用fill_parent来表示填充屏幕的高度。orientation表示该布局管理器中组件的排列方式,取值有horizontal和vertical,也就是水平和竖直排列。 
下面在这个线性布局管理器中创建几个组件: 

Xml代码  

1. <TextView  

2.         android:id="@+id/text1"  

3.         android:layout_width="wrap_content"  

4.         android:layout_height="wrap_content"  

5.         android:text="Sarin" />  

6.     <TextView  

7.         android:id="@+id/text2"  

8.         android:layout_width="wrap_content"  

9.         android:layout_height="wrap_content"  

10.        android:text="辽宁省大连市高新园区七贤岭汇贤园七号" />  

11.    <TextView  

12.        android:id="@+id/text3"  

13.        android:layout_width="wrap_content"  

14.        android:layout_height="wrap_content"  

15.        android:text="腾飞软件园二号楼" />  


    我们设置的布局管理器为水平排列组件,而在这三个组件中我们设置长和宽都是包裹内容,那么运行程序,我们看到如下效果: 
 
    显示的内容挤到了一起,而且text2显示不下折行了,但是text3就完全就完全不见了,那么我们把屏幕切到横屏模式下: 
 
    这次没有问题,都显示出来了。也就达到了我们代码中设置的效果。从前面的文档中我们可以看到LinearLayout是View类的子类,那么我们也就可以在Activity程序中进行动态控制,这在之前的示例中也有涉及,这里我们再进一步来研究。 
    要在程序代码中控制布局,就要设置布局参数,而在XML中我们可以通过属性来直接定义,但在程序中就涉及到了布局参数类LinearLayout.LayoutParams,我们来看下它的文档: 
 
    这是一个静态类,它的继承结构为: 
java.lang.Object 
  ↳ android.view.ViewGroup.LayoutParams 
  ↳android.view.ViewGroup.MarginLayoutParams 
  ↳ android.widget.LinearLayout.LayoutParams
 
    下面来看一下代码: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.view.ViewGroup;  

5. import android.widget.LinearLayout;  

6. import android.widget.TextView;  

7. public class LinearLayoutDemoActivity extends Activity {  

8.     @Override  

9.     public void onCreate(Bundle savedInstanceState) {  

10.        super.onCreate(savedInstanceState);  

11.        LinearLayout layout = new LinearLayout(this);// 定义线性布局管理器  

12.        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  

13.                ViewGroup.LayoutParams.FILL_PARENT,  

14.                ViewGroup.LayoutParams.FILL_PARENT);// 定义布局管理器的参数  

15.        layout.setOrientation(LinearLayout.VERTICAL);// 定义组件的排列方式  

16.        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(  

17.                ViewGroup.LayoutParams.FILL_PARENT,  

18.                ViewGroup.LayoutParams.WRAP_CONTENT);// 定义组件的布局参数  

19.        TextView text = new TextView(this);  

20.        text.setLayoutParams(textParams);// 设置组件的参数  

21.        text.setText("Sarin");// 设置显示文字  

22.        layout.addView(text, textParams);// 增加组件  

23.        super.setContentView(layout, params);// 设置布局管理器  

24.    }  

25.}  


    LinearLayout的构造方法接收Context类型的参数,那么这里毫无疑问是this,下面就是设置布局参数,需要注意的是FILL_PARENT等参数都是ViewGroup.LayoutParams类中的属性。之后我们创建了一个文本组件,并为文本组件设置它的布局管理参数。最后将文本显示组件添加到布局管理器中,最后设置Activity程序使用的布局管理器,那么我们运行程序,得到如下效果: 
 
    说明我们的代码是没有问题的,我们得到了预期的效果。 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

12. Android学习笔记12:框架布局管理器FrameLayout

 

 

 

框架布局管理器是Android布局管理器之一,之前并没有接触过。简单来说,框架布局管理器是将组件都放在屏幕的左上角,所有的组件是层叠显示的。首先来看一下FrameLayout的文档: 
 
    那么它的继承结构为: 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.view.ViewGroup 
  ↳ android.widget.FrameLayout
 
    这和LinearLayout是类似的。下面我们创建一个项目来看看FrameLayout: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="fill_parent"  

5.     android:orientation="vertical" >  

6.     <ImageView  

7.         android:id="@+id/img"  

8.         android:layout_width="wrap_content"  

9.         android:layout_height="wrap_content"  

10.        android:contentDescription="这是一个图片"  

11.        android:src="@drawable/ic_launcher" />  

12.    <TextView  

13.        android:id="@+id/text"  

14.        android:layout_width="wrap_content"  

15.        android:layout_height="wrap_content"  

16.        android:text="这是提示文字" />  

17.    <Button android:id="@+id/btn"   

18.        android:layout_width="wrap_content"  

19.        android:layout_height="wrap_content"  

20.        android:text="这是按钮"/>  

21.</FrameLayout>  


    这里需要注意的就是之前使用的LinearLayout,现在需要改为FrameLayout,也就是我们使用的框架布局管理器。其中放置三个组件,这都是我们很熟悉的内容了,不用过多解释,下面直接运行程序来看一下效果: 
 
    正如我们之前所说的,所有组件均在左上角叠加显示了。 
    和LinearLayout类似,要在程序中控制FrameLayout就会涉及到FrameLayout类和FrameLayout.LayoutParams类,我们就可以通过这两个类来对框架布局管理器进行控制了。下面还是先来看一下FrameLayout.LayoutParams的文档: 
 
    这也是一个静态类,其继承结构为: 
java.lang.Object 
  ↳ android.view.ViewGroup.LayoutParams 
  ↳android.view.ViewGroup.MarginLayoutParams 
  ↳ android.widget.FrameLayout.LayoutParams
 
    下面我们通过代码来控制FrameLayout: 

Java代码  

1. 这也是一个静态类,其继承结构为:  

2. ava.lang.Object  

3.   ↳ android.view.ViewGroup.LayoutParams  

4.        ↳ android.view.ViewGroup.MarginLayoutParams  

5.            ↳ android.widget.FrameLayout.LayoutParams  

6. 下面我们通过代码来控制FrameLayout:  


    可以看出,这和LinearLayout的操作方式是类似的。下面来执行一下程序: 
 
    可以看到,这和我们使用XML布局文件的显示效果是一致的。 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

13. Android学习笔记13:表格布局管理器TableLayout

 

 

 

在Web开发中,我们会接触到形形色色的表格,HTML中的<table>元素为我们实现了表格的实现,可以说,表格是我们使用最多的元素。在Android中,我们可以使用TableLayout来实现表格布局。HTML的表格中使用<tr>来表示表格的一行,类似的,在TableLayout中,我们也有对应的TableRow来表示表格的一行。但在Android中就不往下区分表格单元了,也就是没有相应的<td>。 
    下面首先来看一下TableLayout的文档: 
 
    它是LinearLayout线性布局管理器的子类,其继承结构为: 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.view.ViewGroup 
  ↳ android.widget.LinearLayout 
  ↳ android.widget.TableLayout
 
    从文档中不难看出,要使用TableLayout就离不开TableRow,那么TableRow的文档如下: 
 
    其也是LinearLayout的子类,继承结构如下: 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.view.ViewGroup 
  ↳ android.widget.LinearLayout 
  ↳ android.widget.TableRow
 
    在Eclipse中创建一个项目来说明TableLayout,首先使用XML来定义布局管理器: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="fill_parent"  

5.     android:orientation="vertical" >  

6.     <TableRow>  

7.         <EditText  

8.             android:id="@+id/edit"  

9.             android:layout_width="wrap_content"  

10.            android:layout_height="wrap_content"  

11.            android:text="请输入内容" />  

12.        <Button  

13.            android:id="@+id/btn"  

14.            android:layout_width="wrap_content"  

15.            android:layout_height="wrap_content"  

16.            android:text="搜索" />  

17.    </TableRow>  

18.    <View  

19.        android:id="@+id/hr"  

20.        android:layout_height="2dp"  

21.        android:background="#FDF5E6" />  

22.    <TableRow>  

23.        <TextView  

24.            android:id="@+id/label"  

25.            android:layout_width="wrap_content"  

26.            android:layout_height="wrap_content"  

27.            android:text="请选择您的性别" />  

28.        <RadioGroup  

29.            android:id="@+id/gender"  

30.            android:layout_width="wrap_content"  

31.            android:layout_height="wrap_content"  

32.            android:checkedButton="@+id/male"  

33.            android:orientation="vertical" >  

34.            <RadioButton  

35.                android:id="@+id/male"  

36.                android:text="" />  

37.            <RadioButton  

38.                android:id="@+id/female"  

39.                android:text="" />  

40.        </RadioGroup>  

41.    </TableRow>  

42.</TableLayout>  


    我们创建了一个表格布局管理器,在其中创建了两行,也就是两个TableRow元素。在第一行中,我们放置了两个组件,一个是文本输入组件,给出默认提示文字。第二个是一个按钮,构成一个搜索框。 
    第二个TableRow中我们放置了一个提示文本和一个单选按钮,那么创建一个TextView和一个RadioGroup就行了。在RadioGroup中我们设置RadioButton是纵向排列的,然后默认选中一个。 
    在两个TableRow中间,我们放置了一个<View>元素,可以表示任意的组件,这里我们放置一个水平线,就像HTML中的<hr>元素效果,设置宽度是2dp,颜色为#FDF5E6。 
    运行这段程序,我们就得到如下的显示效果: 
 
    我们在Web开发中使用表格最多的应用就是数据显示了,那么在Android中也会需要用表格来显示数据,下面我们就来看看如何使用TableLayout和TableRow来显示数据,再创建一个布局管理器文件(在layout下创建datadisplay.xml), 
 
    那么在R.java中我们就会得到对应的资源: 

Java代码  

1. package org.pioneer;  

2. public final class R {  

3.     public static final class attr {  

4.     }  

5.     public static final class drawable {  

6.         public static final int ic_launcher=0x7f020000;  

7.     }  

8.     public static final class id {  

9.         public static final int btn=0x7f050001;  

10.        public static final int edit=0x7f050000;  

11.        public static final int female=0x7f050006;  

12.        public static final int gender=0x7f050004;  

13.        public static final int hr=0x7f050002;  

14.        public static final int label=0x7f050003;  

15.        public static final int male=0x7f050005;  

16.    }  

17.    public static final class layout {  

18.        public static final int datadisplay=0x7f030000;  

19.        public static final int main=0x7f030001;  

20.    }  

21.    public static final class string {  

22.        public static final int app_name=0x7f040001;  

23.        public static final int hello=0x7f040000;  

24.    }  

25.}  


    下面来编写这个布局文件,其内容如下: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="match_parent"  

4.     android:layout_height="match_parent" >  

5.     <TableRow>  

6.         <TextView  

7.             android:layout_column="0"  

8.             android:gravity="center_horizontal"  

9.             android:padding="10dp"  

10.            android:text="序号" />  

11.        <TextView  

12.            android:layout_column="1"  

13.            android:gravity="center_horizontal"  

14.            android:padding="10dp"  

15.            android:text="姓名" />  

16.        <TextView  

17.            android:layout_column="2"  

18.            android:gravity="center_horizontal"  

19.            android:padding="10dp"  

20.            android:text="城市" />  

21.        <TextView  

22.            android:layout_column="3"  

23.            android:gravity="center_horizontal"  

24.            android:padding="10dp"  

25.            android:text="性别" />  

26.    </TableRow>  

27.</TableLayout>  


    这里我们仅仅定义出表格的表头,有4列显示内容,那么layout_column表示的就是该列的编号,可以看出,这个编号是从0开始的,同时这是显示样式的水平居中显示,并且都有10dp的间距。修改Activity程序中的布局管理器文件: 

Java代码  

1. super.setContentView(R.layout.datadisplay);  


    运行程序,此时得到如下显示效果: 
 
    那么可以看到,表头我们就做出来了,下面继续设置内容: 

Xml代码  

1. <View  

2.     android:layout_height="2dp"  

3.     android:background="#FDF5E6" />  

4. <TableRow>  

5.     <TextView  

6.         android:layout_column="0"  

7.         android:gravity="center_horizontal"  

8.         android:padding="5dp"  

9.         android:text="001" />  

10.    <TextView  

11.        android:layout_column="1"  

12.        android:gravity="center_horizontal"  

13.        android:padding="10dp"  

14.        android:text="Sarin" />  

15.    <TextView  

16.        android:layout_column="2"  

17.        android:gravity="center_horizontal"  

18.        android:padding="10dp"  

19.        android:text="辽宁省大连市高新园区七贤岭汇贤园七号腾飞软件园二号楼" />  

20.    <TextView  

21.        android:layout_column="3"  

22.        android:gravity="center_horizontal"  

23.        android:padding="10dp"  

24.        android:text="" />  

25.</TableRow>  

26.<TableRow>  

27.    <TextView  

28.        android:layout_column="0"  

29.        android:gravity="center_horizontal"  

30.        android:padding="5dp"  

31.        android:text="002" />  

32.    <TextView  

33.        android:layout_column="1"  

34.        android:gravity="center_horizontal"  

35.        android:padding="10dp"  

36.        android:text="Tom" />  

37.    <TextView  

38.        android:layout_column="2"  

39.        android:gravity="center_horizontal"  

40.        android:padding="10dp"  

41.        android:text="北京市" />  

42.    <TextView  

43.        android:layout_column="3"  

44.        android:gravity="center_horizontal"  

45.        android:padding="10dp"  

46.        android:text="" />  

47.</TableRow>  


    我们设置了一条水平横线,下面是两条数据的显示,那么运行程序,我们会看到这样的显示效果: 
 
    显示不下不要紧,我们切换到横屏模式下再看: 
 
    但是问题仍然存在,因为某一个文本太长了,并且我们设置了水平居中显示的效果,所以后面的内容都显示不下消失了,显然这么显示不合理,需要列可以折行显示内容,设置如下内容: 

Xml代码  

1. 下内容:  

2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="match_parent"  

4.     android:layout_height="match_parent"   

5.     android:shrinkColumns="2">  


    在TableLayout元素上我们设置一个shrinkColumns属性,就是设置哪一列可以折行显示,这里是第三列需要折行显示,设置完成,显示效果如下: 
 
    我们就得到了预期的效果。表格布局中还可以设置隐藏列,那么需要在TableLayout上设置如下属性: 

Xml代码  

1. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  

2.     android:layout_width="match_parent"  

3.     android:layout_height="match_parent"   

4.     android:shrinkColumns="2"  

5.     android:collapseColumns="0,3">  


    这里我们设置第一列和第四列为不显示状态,那么就得到如下显示效果: 
 
    和前面两个布局管理器类似,我们也可以通过程序来控制表格布局管理器。不难想到这里我们需要四个类来完成,分别是TableLayout,TableLayout.LayoutParams,TableRow,TableRow.LayoutParams 
    下面来看一下TableLayout.LayoutParams的文档: 
 
    可以看出TableLayout.LayoutParams继承自LinearLayout.LayoutParams,其继承结构如下: 
java.lang.Object 
  ↳ android.view.ViewGroup.LayoutParams 
  ↳android.view.ViewGroup.MarginLayoutParams 
  ↳ android.widget.LinearLayout.LayoutParams 
  ↳ android.widget.TableLayout.LayoutParams
 
    下面是TableRow.LayoutParams,其实它们都是类似的: 
 
    它的继承结构为: 
java.lang.Object 
  ↳ android.view.ViewGroup.LayoutParams 
  ↳android.view.ViewGroup.MarginLayoutParams 
  ↳ android.widget.LinearLayout.LayoutParams 
  ↳ android.widget.TableRow.LayoutParams
 
    下面我将在XML布局管理文件中配置的数据表格用程序来进行编写,那么就需要准备数据,表格数据可以看作是二维表,那么很自然想到,在数据量不大时可以使用而为数组来表示,其代码如下: 

Java代码  

1. package org.pioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.view.ViewGroup;  

5. import android.widget.TableLayout;  

6. import android.widget.TableRow;  

7. import android.widget.TextView;  

8. public class TableLayoutDemoActivity extends Activity {  

9.     private String tableData[][] = new String[][] { { "序号""姓名""地址""性别" },  

10.            { "001""Sarin""辽宁省大连市高新园区七贤岭汇贤园七号腾飞软件园二号楼""" },  

11.            { "002""Tom""北京市""" } };// 定义显示的数据  

12.    @Override  

13.    public void onCreate(Bundle savedInstanceState) {  

14.        super.onCreate(savedInstanceState);  

15.        TableLayout layout = new TableLayout(this);// 定义布局管理器  

16.        TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(  

17.                ViewGroup.LayoutParams.FILL_PARENT,  

18.                ViewGroup.LayoutParams.FILL_PARENT);// 定义布局管理器参数  

19.        layout.setBackgroundResource(R.drawable.ic_launcher);// 设置背景图片  

20.        for (int i = 0; i < tableData.length; i++) {  

21.            TableRow row = new TableRow(this);// 定义TableRow  

22.            for (int j = 0; j < tableData[i].length; j++) {  

23.                TextView text = new TextView(this);// 定义TextView组件  

24.                text.setText(tableData[i][j]);// 设置TextView的显示文字  

25.                row.addView(text, j);// TableRow中添加TextView组件  

26.            }  

27.            layout.addView(row);// 向布局管理器中添加表格行  

28.        }  

29.        super.setContentView(layout, layoutParams);// 设置布局管理器  

30.    }  

31.}  


    对于二维数组的遍历必然是嵌套的循环来进行,运行程序,我们得到如下效果: 
 
    可以看到,我们之前设置的显示效果都不存在了,当然这可以通过代码继续设置样式,但该代码已经相当的复杂了,如果继续修改程序,那程序必然显得十分臃肿。所以对复杂布局我们采用XML文件的配置方式来进行是比较好的方法。 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14. Android学习笔记14:相对布局管理器RelativeLayout

 

 

 

相对布局管理器是基于一个参考点而言的布局管理器。就像Web开发中的相对路径的概念,是基于一定的参考点而创建的。在Android中的相对布局管理器就是在一个参考点的四周(上,下,左,右)布局的管理器。 
    下面来看一下RelativeLayout的文档: 
 
    它的继承结构为: 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.view.ViewGroup 
  ↳ android.widget.RelativeLayout
 
    下面在Eclipse中新建一个项目来看一下相对布局管理器RelativeLayout的使用: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="fill_parent"  

5.     android:orientation="vertical" >  

6.     <ImageView  

7.         android:id="@+id/img1"  

8.         android:layout_width="wrap_content"  

9.         android:layout_height="wrap_content"  

10.        android:src="@drawable/ic_launcher" />  

11.    <ImageView  

12.        android:id="@+id/img2"  

13.        android:layout_width="wrap_content"  

14.        android:layout_height="wrap_content"  

15.        android:src="@drawable/google_plus"  

16.        android:layout_toRightOf="@+id/img1" />  

17.</RelativeLayout>  


    我们在main.xml中将布局管理器声明为RelativeLayout,之后创建了两个ImageView组件用来显示两幅图片,其中在第二个ImageView组件上设置了layout_toRightOf属性,也就是设置相对于某组件的右侧,这里填入的是组件ID的值,那么这里也就是说我们的img2相对于img1的位置是右侧。下面运行程序,我们看到如下效果: 
 
    很明显,第二幅图片放置在了第一副图片的右侧,下面往代码中再加入一个TextView组件: 

Xml代码  

1. <TextView android:id="@+id/txt"  

2.     android:layout_width="wrap_content"  

3.     android:layout_height="wrap_content"  

4.     android:text="这里是一些显示文字"  

5.     android:layout_below="@+id/img2"/>  


    这个组件也很简单,我们设置了layout_below属性,说明要放置在第二幅图片的下面,那么运行程序,我们得到如下的显示效果: 
 
    没有问题,文字确实在第二幅片的下面了,但是却顶头显示了,如果第一副图片小于第二幅图片,是会产生覆盖效果的,我们调整位置来看一下,调整代码为: 

Xml代码  

1. <ImageView  

2.     android:id="@+id/img1"  

3.     android:layout_width="wrap_content"  

4.     android:layout_height="wrap_content"  

5.     android:src="@drawable/ic_launcher"  

6.     android:layout_toRightOf="@+id/img2" />  

7. <ImageView  

8.     android:id="@+id/img2"  

9.     android:layout_width="wrap_content"  

10.    android:layout_height="wrap_content"  

11.    android:src="@drawable/google_plus" />  

12.<TextView android:id="@+id/txt"  

13.    android:layout_width="wrap_content"  

14.    android:layout_height="wrap_content"  

15.    android:text="这里是一些显示文字"  

16.    android:layout_below="@+id/img1"/>  


    这里不再解释代码的含义,直接运行,我们看到: 
 
    文字覆盖第一副图片显示了,那么需要继续对它进行设置: 

Xml代码  

1. <TextView android:id="@+id/txt"  

2.     android:layout_width="wrap_content"  

3.     android:layout_height="wrap_content"  

4.     android:text="这里是一些显示文字"  

5.     android:layout_below="@+id/img1"  

6.     android:layout_toRightOf="@+id/img2"/>  


    再次运行程序,我们可以看到如下效果: 
 
    文字就在img1的下面并且在img2的右侧了。此时文字的下侧和img2的右侧还有一定空间,我们再放置一个Button组件: 

Xml代码  

1. <Button   

2.     android:id="@+id/btn"  

3.     android:layout_width="wrap_content"  

4.     android:layout_height="wrap_content"  

5.     android:text="按钮"  

6.     android:layout_below="@+id/txt"  

7.     android:layout_toRightOf="@+id/img2"/>  


    再次运行程序,我们就得到了如下效果: 
 
    和其它布局管理器一样,我们可以通过Java代码来实现对相对布局管理器的控制,下面首先来看一下RelativeLayout.LayoutParams的文档: 
 
    其继承结构为: 
java.lang.Object 
  ↳ android.view.ViewGroup.LayoutParams 
  ↳android.view.ViewGroup.MarginLayoutParams 
  ↳ android.widget.RelativeLayout.LayoutParams
 
    只是在代码中控制相对布局管理器时需要设置一些规则,也就是我们上面看到的layout_toRightOf和layout_below等,下面来看一下代码: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.view.ViewGroup;  

5. import android.widget.EditText;  

6. import android.widget.RelativeLayout;  

7. public class RelativeLayoutDemoActivity extends Activity {  

8.     @Override  

9.     public void onCreate(Bundle savedInstanceState) {  

10.        super.onCreate(savedInstanceState);  

11.        super.setContentView(R.layout.main);// 读取已有的布局管理器  

12.        RelativeLayout relativeLayout = (RelativeLayout) super  

13.                .findViewById(R.id.rLayout);// 获取相对布局管理器rLayout  

14.        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(  

15.                ViewGroup.LayoutParams.FILL_PARENT,  

16.                ViewGroup.LayoutParams.FILL_PARENT);// 设置布局管理器参数  

17.        params.addRule(RelativeLayout.BELOW, R.id.btn);// 设置放置规则  

18.        EditText edit = new EditText(this);// 创建EditText组件  

19.        relativeLayout.addView(edit,params);  

20.    }  

21.}  


    编写代码之前,我们需要在main.xml中为我们的布局管理器添加ID属性,也就是rLayout,之后我们可以在代码中对它进行控制,这里我们在已有的布局管理器之中继续添加组件,也就是要往按钮下放置一个编辑框,那么我们设置布局管理器参数都为FILL_PARENT,就是要填充整个屏幕,然后规则定位在btn的下侧,之后往布局管理器中添加组件,运行程序,我们就可以看到: 
 

 

 

 

 

 

 

 

 

 

15. Android学习笔记15:绝对布局管理器AbsoluteLayout

 

 

 

 有相对布局管理器,对应的,我们还有绝对布局管理器。这和CSS中的定位布局是类似的。CSS中我们可以通过相对布局管理器对HTML元素进行布局,也可以通过绝对布局定位通过指定top,left等属性来为元素进行布局。而在Android之中,我们还有一个绝对布局管理器可以做到同样的效果。 
    首先说明绝对布局管理器是已经在Android 2.3之后被废弃了,是不建议使用的。但绝对布局管理器却是布局管理器中最早的一种,下面来看一下绝对布局管理器的文档: 

 
    文档已经说明,这是被废弃的布局管理器,而它的继承结构为: 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.view.ViewGroup 
  ↳ android.widget.AbsoluteLayout
 
    绝对布局管理器的含义就似乎采用坐标进行定位,我们的屏幕是二维结构,那么绝对布局管理器就按照x和y坐标进行定位,坐标的原点位于屏幕左上角。 
    下面在Eclipse中新建一个项目来看一下绝对布局管理器,要注意我们创建的版本为Android 2.2(API Level 8): 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="fill_parent"  

5.     android:orientation="vertical" >  

6.     <TextView  

7.         android:id="@+id/txt"  

8.         android:layout_width="wrap_content"  

9.         android:layout_height="wrap_content"  

10.        android:layout_x="40dp"  

11.        android:layout_y="10dp"  

12.        android:text="这里是一些显示信息" />  

13.</AbsoluteLayout>  


    这里我们创建了一个绝对布局管理器,其中只有一个TextView组件用于文字显示,这里我们的布局属性设置了layout_x和layout_y,也就是设置坐标,那么运行程序,我们可以看到如下效果: 

 

    那么x坐标就是40dp,y坐标是10dp,从这个坐标点开始放置我们设置的组件。下面再放置一个图片: 

Xml代码  

1. <ImageView  

2.     android:id="@+id/img"  

3.     android:layout_width="wrap_content"  

4.     android:layout_height="wrap_content"  

5.     android:layout_x="60dp"  

6.     android:layout_y="60dp"  

7.     android:src="@drawable/ic_launcher" />  


    都是很简单的设置了,下面直接运行程序,我们可以看到如下效果: 

 

    可以看到绝对定位管理器就是根据坐标来进行布局的,这是不推荐的,因为我们无法预知运行程序的设备实际情况,如果我们定义坐标,很可能我们的程序是不能正常显示的。所以在Android 2.3之后该布局管理器就被废除了。 

 

 

 

 

 

 

16. Android学习笔记16:布局管理器的嵌套

 

 

 

布局管理器的嵌套就是将多种布局管理器混合使用,以达到复杂布局的排版效果。如果一个布局页面效果复杂,可能使用一种布局管理器无法完成,那么我们就需要将多种布局管理器嵌套起来以达到显示效果。在Web开发中,编写的CSS基本都是设置嵌套元素的样式的,这个理念是类似的。 
    几种布局管理器都已经介绍过了,我们直接在Eclipse中建立新的项目来说明: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="fill_parent"  

5.     android:orientation="vertical" >  

6.     <TextView  

7.         android:id="@+id/txt"  

8.         android:layout_width="fill_parent"  

9.         android:layout_height="wrap_content"  

10.        android:gravity="center"  

11.        android:text="文字提示信息" />  

12.    <LinearLayout  

13.  xmlns:android="http://schemas.android.com/apk/res/android"  

14.        android:layout_width="fill_parent"  

15.        android:layout_height="wrap_content"  

16.        android:gravity="center"  

17.        android:orientation="horizontal" >  

18.        <ImageView  

19.            android:id="@+id/img1"  

20.            android:layout_width="wrap_content"  

21.            android:layout_height="wrap_content"  

22.            android:src="@drawable/ic_launcher" />  

23.        <ImageView  

24.            android:id="@+id/img2"  

25.            android:layout_width="wrap_content"  

26.            android:layout_height="wrap_content"  

27.            android:src="@drawable/ic_launcher" />  

28.        <ImageView  

29.            android:id="@+id/img3"  

30.            android:layout_width="wrap_content"  

31.            android:layout_height="wrap_content"  

32.            android:src="@drawable/ic_launcher" />  

33.    </LinearLayout>  

34.</LinearLayout>  


    相对以前的代码,这个有些复杂,我们一层一层来看,首先外部是个线性布局(该布局管理器垂直放置各个元素),放置了一个文本显示组件TextView,并且设置这个组件为居中显示。之后嵌套了一个线性布局管理器(该布局管理器设置水平放置各个元素),嵌套的布局管理器中放置了三幅图片,那么运行该布局管理器来看看显示效果: 

 

    下面我们继续嵌套,在内部线性布局管理器之下,我们平行放置一个表格布局管理器: 

Xml代码  

1. <TableLayout  

2.  xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content"  

5.     android:gravity="center"  

6.     android:orientation="horizontal" >  

7.     <TableRow>  

8.         <EditText  

9.             android:id="@+id/edit"  

10.            android:layout_width="wrap_content"  

11.            android:layout_height="wrap_content"  

12.            android:text="输入关键字进行搜索" />  

13.        <Button  

14.            android:id="@+id/btn"  

15.            android:layout_width="wrap_content"  

16.            android:layout_height="wrap_content"  

17.            android:text="搜索" />  

18.    </TableRow>  

19.</TableLayout>  


    这里面也没有什么可多说的,直接运行程序,我们可以看到: 

 

    只是在嵌套布局管理器时我们要注意设置各个布局管理器的layout_height为包裹高度,而不能设置成为填充屏幕,否则该布局管理器就会占据剩余屏幕整个内容,而其它的布局管理器将无法显示。 
    在嵌套布局管理器时,页面的排版已经非常的复杂了,而若想使用Java代码对其进行控制,这个难度就已经很大了,所以在复杂布局管理器下不建议再编写Java代码来控制了,使用XML文件来布局是比较简单的方式。

 

 

 

 

 

 

 

 

17. Android学习笔记17:中级视图组件DatePicker和TimePicker

 

 

 

 HTML5出现之前,我们在Wweb开发中并没有现成的日期选择器和时间选择器来用,都是通过第三方组件来引入的,比如jQuery UI的DatePicker组件。而在Android中,我看可以使用系统为我们原生提供的日期选择器DatePicker和时间选择器TimePicker。 
    首先来看一下日期选择器DatePicker的文档: 

 

    其继承结构为: 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.view.ViewGroup 
  ↳ android.widget.FrameLayout 
  ↳ android.widget.DatePicker
 
    很容易看出它是FrameLayout的子类,也就是说它的内部实现使用了框架布局。下面在Eclipse中新建项目看看日期选择器的使用: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="fill_parent"  

5.     android:orientation="vertical" >  

6.     <DatePicker  

7.         android:id="@+id/datePicker1"  

8.         android:layout_width="fill_parent"  

9.         android:layout_height="wrap_content" />  

10.</LinearLayout>  


    代码非常的简单,我们就是定义了一个日期选择器DatePicker,仅仅设置了高度和宽度两个属性,下面直接运行代码,我们来看看显示效果: 

 

    因为纵向显示不下,我们横向显示。可以看出日期选择器的效果了,此时显示的风格是英文下的月-日-年,并且月份使用的是英文简写,这不符合中国的使用习惯,我们需要更改设置。在Android模拟器中,选择Settings,之后选择语言和键盘设置: 

 

    我们进入选择语言,然后选择语言为简体中文,之后再次运行我们的程序,就会看到如下的显示效果: 

 

    此时的日期格式已经按照我们的习惯来显示了。 
    下面我们看看如何在程序中来控制日期选择器,首先再定义的日期选择器,以供操作: 

Xml代码  

1. <DatePicker  

2.     android:id="@+id/datePicker2"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="wrap_content" />  


    之后,编写Java程序代码: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.widget.DatePicker;  

5. public class DatePickerDemoActivity extends Activity {  

6.     private DatePicker datePicker2 = null;  

7.     @Override  

8.     public void onCreate(Bundle savedInstanceState) {  

9.         super.onCreate(savedInstanceState);  

10.        super.setContentView(R.layout.main);// 设置使用的布局管理器  

11.        datePicker2 = (DatePicker) super.findViewById(R.id.datePicker2);// 获取DatePicker组件  

12.        datePicker2.updateDate(2006826);// 设置日期  

13.    }  

14.}  


    这里的操作也很简单,获取到日期选择器后设置一下日期即可,我们设置为2006-8-26,运行程序,来看看显示效果: 

 

    此时显示的效果为2006-9-26,说明在月份的设置上,显示的值实际比我们设置的值自动加了1。下面我们将程序安装到Android设备上来看看具体效果,这里的运行环境为Motorola Defy+ 2.3.7 MIUI: 

 

    可以看到显示的效果和模拟器中的略有不同,而且我们对datePicker2的设置没有起作用。这可能是具体ROM的不同处理方式,而在模拟器中的显示是没有问题的,我们只要会用日期选择器就行了。 
    下面来看看时间选择器TimePicker,日期选择器选择的是年月日,那么时间选择器就是选择时分了,首先来看看TimePicker的文档: 

 

    其继承结构为: 
java.lang.Object 
  ↳ android.view.View 
  ↳ android.view.ViewGroup 
  ↳ android.widget.FrameLayout 
  ↳ android.widget.TimePicker
 
    很显然,它也是FrameLayout的子类,下面新建项目来看看时间选择器的用法: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayou  

3. xmlns:android="http://schemas.android.com/apk/res/android"  

4.     android:layout_width="fill_parent"  

5.     android:layout_height="fill_parent"  

6.     android:orientation="vertical" >  

7.     <TimePicker  

8.         android:id="@+id/timePicker"  

9.         android:layout_width="fill_parent"  

10.        android:layout_height="wrap_content" />  

11.</LinearLayout>  


    代码也很简单,不用多说什么,直接运行来看看效果: 

 

    默认显示是12小时制,中文显示上午/下午,英文环境显示AM/PM,这都是常识性的知识了,下面我们将显示风格设置为24小时制: 

Java代码  

1. package org.ourpioneer;  

2. import android.app.Activity;  

3. import android.os.Bundle;  

4. import android.widget.TimePicker;  

5. public class TimePickerDemoActivity extends Activity {  

6.     private TimePicker timePicker = null;  

7.     @Override  

8.     public void onCreate(Bundle savedInstanceState) {  

9.         super.onCreate(savedInstanceState);  

10.        super.setContentView(R.layout.main);  

11.        timePicker = (TimePicker) super.findViewById(R.id.timePicker);// 获取时间选择器  

12.        timePicker.setIs24HourView(true);// 设置为24小时制  

13.        timePicker.setCurrentHour(12);// 设置当前小时  

14.        timePicker.setCurrentMinute(30);// 设置当前分钟  

15.    }  

16.}  


   下面运行程序来看看效果: 

 

    那么我们看到时间已经调整为24小时制下的12:30了。要注意的日期时间的设置和国际化是密切相关的。 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值