android application: introduction

152
Android 与底

Upload: jollen-chen

Post on 10-May-2015

13.076 views

Category:

Education


1 download

DESCRIPTION

Jollen's Android Training. Introducing App Dev - China Shanghai (Day2-6hr)

TRANSCRIPT

Page 1: Android Application: Introduction

Android 应用开发与底层技术

Page 2: Android Application: Introduction

Jollen’s Consulting

与优质的课程平台商合作,专注课程研究与开发,致力创造教育训练的价值

课程供应与开发者,致力于价值创造

详细资讯: www.jollen.org/consulting

专业课程供应

Page 3: Android Application: Introduction

主讲:

陈俊宏 Jollen Chen <[email protected]>

资深Embedded Linux顾问与讲师,在Embedded Linux以及Linux驱动程式方面

有丰富的经验;同时也负责Openmoko在大中华区的行销与推广,在过去二年,带领

Openmoko深耕教育市场,对开放手机平台的教育与推广不遗余力。目前则是专注于

Android OS底层的技术研究,以及提供Android专案设计服务

简介:知名开源专案Openmoko大中华区负责人、台湾教育部顾问室嵌入式软体联

盟-Android种子师资与产学合作培训计画发起人暨讲师、北京中关村开放手机联盟-共

同发起人、目前参与多项Android研发计画

专长:Android porting (s3c244x/s3c64x0)、Linux驱动开发、嵌入式Linux架

构设计、底层技术导入与顾问

Jollen的部落格 - www.jollen.org/blog

Jollen的工作室 - www.jollen.org/consulting  提供培訓與項目外包服務

www.jollen.org/consulting

Page 4: Android Application: Introduction

Day2: 應用模式與應用開發

主讲人:Jollen Chen (陈俊宏)

Email:[email protected]

Blog:jollen.org/blog

课程时间:2009.08.29-09.01

上课地点:上海

Page 5: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

1. Android開發環境建置

5

经常性变动课件我们将提供您电子档

Page 6: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Eclipse IDE

Android模拟器(AVD)

Android Development Kit - ADT

Android Debug Bridge - ADB

Hierarchy Viewer -协助UI设计与布局

Android开发工具

5

Page 7: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Android模拟器采用Qemu

加入了device skin功能

支援GSM及SMS命令

AVD

Android模拟器

7

Page 8: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

加入许多修改:power management、RAM console、Android gadget、Android kernel debug与Android IPC。

由git.android.com取得patchO p e n m o k o针对 A R M v 4平台( N e o

FreeRunner)做了完整移植

Android Linux Kernel

8

Page 9: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

采用Eclipse整合开发环境

整合target device与除错环境

整合Android应用程式的建立、开发、执行

测试与错误功能至Eclipse整合模拟器(AVD)

Android Development Kit

9

Page 10: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

采用Eclipse整合开发环境

整合target device与除错环境

存放于Android SDK tools/目录下

DDMS

Android Debug Bridge

10

Page 11: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

http://www.eclipse.org/downloads/

下载Eclipse并设定SDK路径

11

Page 12: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Hierarchy Viewer

12

Page 13: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

建立Android专案

13

Page 14: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

建立com.moko.hello类别

14

Page 15: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

主程式: HelloAndroid类别

Page 16: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

建立AVD

16

Page 17: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

执行专案:选择AVD

Page 18: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

2. Android應用程式開發模式

18

Page 19: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

package com.moko.hello;

import com.moko.hello.R;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;

public class HelloMoko extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }}

Activity类别

Page 20: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

在onCreate()里建立View

Android 的 Activity

public class HelloMoko extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }}

20

Page 21: Android Application: Introduction
Page 22: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

与使用者互动的物件

负责建立视窗

透过View放置UI

Activity在foreground执行

Android 的 Activity

22

Page 23: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

执行中的Activity部份画面被其他Activity盖掉时,该Activity便被暂停(paused),被暂停的Activity在系统记亿体不足时,便会被清除(kill)

执行中的Activity全部画面都被其他Activity取代时,该Activity便被停止(stopped),当系统需要记忆体时,停止中的Activity会先被系统清除

Android 的 Activity

23

Page 24: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

修改 XML Attribute

Page 25: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

修改attribute: 范例

使用「android:autoLink」属性控制「是否要自动将网址转换为可点击的URL文字」

透过「autoLink」属性,并将此属性设定为「web」即可做出我们想要的功能

25

Page 26: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/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="Jollen's Blog - http://www.jollen.org/blog" android:autoLink="web" /></LinearLayout>

修改 main.xml

Page 27: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

直接撰写程式码

Android 的 Activity

public class HelloMoko extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

TextView tv = new TextView(this); tv.setText("Hello Moko"); setContentView(tv);

}}

27

Page 28: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

R.java: Resource Index

Page 29: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

R.java是由Android Development Kit所自动产生的资源索引档

「R.layout」类别则是UI布局的索引类别

R.layout类别里的「main」成员就是Android应用程式的「主布局索引」

Android的R类别

29

Page 30: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

低阶的执行程式

系统层(kernel)的部份

一个a p k套件里的所有程式,都是在一个process里执行

A n d r o i d应用程式都有一个自已的L i n u x process在Android系统里,process的生命周期并不是

直接由Android应用程式本身来决定,而是由系

统来决定

Android 的 Process

30

Page 31: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

package com.moko.hello;

import android.app.Service;import android.content.Intent;import android.os.IBinder;

public class MokoService extends Service { @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId);

}}

Service类别

Page 32: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

没有UIService是一个background process

Service物件以separated process的方式执行,这表示Service与UI(Activity)并不在同一个process里执行,而是个自在不同的process执行。

Android应用程式是在Activity里启动与停止Service

Android的Service类别

32

Page 33: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.moko.player" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MokoPlayer" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MokoService"> <intent-filter> <action android:name="com.moko.player.START_MUSIC" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service> </application></manifest>

定义Service接收的Intent

Page 34: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

public class MokoPlayer extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startService(new Intent ("com.moko.player.START_MUSIC")); }}

启动Service

Page 35: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

public class MokoService extends Service {

@Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onStart(Intent intent, int startId) { } @Override public void onDestroy() { }}

onStart(), onDestroy method

Page 36: Android Application: Introduction
Page 37: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Activity / Servicemain.xml

View

AndroidManifest.xml -描述Android应用程式Intent / BroadcastReceiver

Android程式设计的特点

37

Page 38: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

3. Android UI設定-View的觀念

38

Page 39: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

手机的UIView负责绘制UI与处理事件(event)

Android应用程式的UI从程式码的角度来看,就

是一颗「view tree」可以利用直接撰写程式码,或是透过「XML

layout」档的方式,来安排应用程式的view tree

Android 的 View

39

Page 40: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Page 41: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

foreground processvisible process

service process

background processempty process

Android 的 Process Types

41

Page 42: Android Application: Introduction

Source: Android SDK

Page 43: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

android.widget.DatePicker android.widget.TimePickerAndroid提供的Widget

Page 44: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

android.widget.Spinner android.widget.AutoCompleteTextView

Android提供的Widget

Page 45: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

android.widget.Buttonandroid.widget.EditTextandroid.widget.CheckBoxandroid.widget.RadioButton

android.widget.ImageButton

Page 46: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

android.widget.Gallery android.widget.ImageSwitcherAndroid提供的Widget

Page 47: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

android.widget.ListView android.widget.GridViewAndroid提供的Widget

Page 48: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

android.widget.LinearLayout(Horizontal) (Vertical)

Android提供的Widget

Page 49: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

android.widget.RelativeLayout android.widget.TableLayoutAndroid提供的Widget

Page 50: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

4. Android UI設計-基本Widget

50

Page 51: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

当Activity被建立后,我们呼叫setContentView()方法,将主要的UI(R.layout.main)显示在视窗上。 R.layout.main索引到一个TextView物件,此物件定义于main.xml

R.layout.main

public class HelloMoko extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }

51

Page 52: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

'android:layout_width' - View的宽度'android:layout_height' - View的高度

'android:text' - TextView所要显示的文字

修改 XML Layout

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />

52

Page 53: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

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

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">

<TableRow> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="www.jollen.org" android:padding="3dip" android:autoLink="web" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="www.google.com" android:autoLink="web" /> </TableRow>

</TableLayout>

再战Android: TableLayout

Page 54: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

package com.moko.layout;

import com.moko.layout.R;

import android.app.Activity;import android.os.Bundle;

public class HelloLayout extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }}

TableLayout: 主程式

Page 55: Android Application: Introduction
Page 56: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

UI的观念与变革

自从有了图形化应用程式之后,对话盒(dialog)

一直是元老级的元件(widget)

对话盒仍然是手机介面的重要图形元件

56

Page 57: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

UI的观念与变革:手机UI

在Apple的iPhone问世后,触控萤幕(touch

screen)一直是智慧型手机的标准规格

传统的滑鼠点击(click)式介面,并不完全适

合手指触控的操作方式

手机触控萤幕尺吋较小,因此手机应用程式的

介面设计,已经与传统的桌面环境相当不同

57

Page 58: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

UI的观念与变革:基本UI元件

Android的元件库考量了小尺吋的触控萤幕

基本元件的设计上Android也为使用者做了很体

贴的考量

以Android手机应用程式来说,经常使用的元件

已经不再像过去的点击式系统那么多又复杂

以使用性的角度来看,常被使用的元件如下

选单(Menu)

对话盒(Dialog)

快显讯息(Toast)

58

Page 59: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Android 的 Widget

TextView是一种Widget

Android提供许多好用的Widget

Android Cupcake提供了App Widget API,

这是一个重要的功能,实用的桌面应用

59

Page 60: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

建立 Menu 的 resource file

在Android专案的res/目录下新增一个menu/子

目录,然后建立options_menu.xml文件

60

Page 61: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting 61

Page 62: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting 62

Page 63: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_message" android:title="New Message" /> <item android:id="@+id/quit" android:title="Quit" /></menu>

建立 Menu

63

Page 64: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

如何产生Menu?

在onCreateOptionsMenu()事件里以

MenuInflater类别将定义好的选单加入应用程式

使用到二个类别:Menu与MenuInflater,因此

记得import这二个套件

64

Page 65: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.options_menu, menu); return true;}

建立Menu程式实作

65

Page 66: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting 66

Page 67: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

如何处理Menu触控?

透过onOptionsItemSelected()事件

当此事件被回呼时,Android框架传入被触压

的选项物件,其类别为MenuItem

我们所定义的选单UI会被放到R类别里

67

Page 68: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

import android.view.MenuItem;

public boolean onOptionsItemSelected(MenuItem item) { return true; }

处理触控的程式片断

68

Page 69: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

package com.moko.hellomenu;

public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int new_message=0x7f060000; public static final int quit=0x7f060001; } public static final class layout { public static final int main=0x7f030000; } public static final class menu { public static final int options_menu=0x7f050000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; }}

R.menu.options_menu

69

Page 70: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

如何取得选取项目?

呼叫MenuItem的getItemId()方法,可取得该选

项的ID

如此一来便能得知使用者所触压的选项

70

Page 71: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

public boolean onOptionsItemSelected(MenuItem item) { int item_id = item.getItemId(); switch (item_id){ case R.id.new_message: break; case R.id.quit: break; default: return false; } return true; }

处理MenuItem

71

Page 72: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

什么是Toast?

Toast是Android提供「快显讯息」类别

使用时请import android.widget.Toast套件

配合上述的选单范例,我们将

onOptionsItemSelected()回呼函数实作做修改

72

Page 73: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

public boolean onOptionsItemSelected(MenuItem item) { int item_id = item.getItemId(); switch (item_id){ case R.id.new_message: Toast.makeText( this, "Please enter your message." + " Your message is at max 255 characters.", Toast.LENGTH_LONG).show(); break; case R.id.quit: Toast.makeText( this, "Going to quit.", Toast.LENGTH_LONG).show(); break; default: return false; }

使用 Toast

73

Page 74: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting 74

Page 75: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

什么是Dialog?

对话盒

一个让应用程式与使用者「对话」的元件。应用

程式透过对话盒与使用者进行下述的对话:

询问问题:使用者回答Yes/No

询问偏好:使用者选择自已偏好的项目,

可以是单选,也可以是复选

说明状态:让使用者知道应用程式目前的

状态,例如:显示「处理中」、「载入

中」等讯息

75

Page 76: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

android.app.Dialog

Android提供的对话盒物件为

android.app.Dialog

实作上继承自Dialog的AlertDialog物件是最基

本的对话盒物件

使用AlertDialog对话盒,可以询问使用者问

题,也可以询问使用者偏好

76

Page 77: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

使用情境(senario)使用者按下Menu键

使用者触控“New Message”选项

出现对话盒、询问使用者“Yes/No”

77

Page 78: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

如何实作?

由以上的使用情境来看,应该在

onOptionsItemSelected()里判断到R.id.new_message项目时,在UI上建立一个对话盒

78

Page 79: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

public boolean onOptionsItemSelected(MenuItem item) { int item_id = item.getItemId(); switch (item_id){ case R.id.new_message: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Also post your message to Twitter?"); builder.setCancelable(false); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); AlertDialog alert = builder.create(); alert.show(); break; case R.id.quit: Toast.makeText( this, "Going to quit.", Toast.LENGTH_LONG).show(); break; default: return false; } return true; }

Dialog实作范例

79

Page 80: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Dialog实作重点解说(1)

产生AlertDialog.builder物件(dialog

builder),这是一个用来建立对话盒内容的产生

器物件

设定dialog builder的显示讯息-

builder.setMessage()

设定对话盒能不能被「取消」-

builder.setCancelable()

80

Page 81: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Dialog实作重点解说(2)

利用dialog builder在对话盒里产生二个按钮

「Yes与No」-builder.setPositiveButton()与

builder.setNegativeButton()

使用dialog builder来建立AlertDialog物件,

AlertDialog是真正的对话盒物件-builder.create()

将AlertDialog显示在UI上-alert.show()

81

Page 82: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Page 83: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

按键事件处理概述

呼叫builder.setPositiveButton()方法建立一个「正面(Yes)」的按钮,参数说明如下:

第一个参数:显示在按钮上的文字 第二个参数:指定click listener

每一个按钮都需要一个click listener,当使用者触压按钮时,click listener便被回呼。 android.content.DialogInterface类别提供click listener物件

83

Page 84: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

5. Android的靈魂-Intent與Broadcast

84

Page 85: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Intent(意图)类似于Notification、事件通知向Android系统表达你的Intent

Intent将由合适的应用程式做处理

「我想打电话...」就是一个意图把你的意图传达出去!

Android 的 Intent

85

Page 86: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Web浏览打电话

传voice mail

开启Google map、显示地图

开启Street View

标准的Intents

86

Page 87: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.moko.hello" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloMoko" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>

试图让Activity无法处理启动企图

87

Page 88: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

startService(Intent service) -送出Intent以启动Service传voice mail

开启Google map、显示地图

开启Street View

android.content.Intent

88

Page 89: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

public class MokoPlayer extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startService(new Intent ("com.moko.player.START_MUSIC")); }}

打电话、丢出android.intent.action.DIAL意图

Page 90: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Intent包含action與dataandroid.intent.action.VIEW - tel:0988082

Page 91: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

import android.app.Activity;import android.os.Bundle;import android.content.Intent;import android.net.Uri;

public class MokoPlayer extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent dial = new Intent(); dial.setAction("android.intent.action.VIEW"); dial.setData(Uri.parse("tel:0988082134")); startActivity(dial); }}

我的意图:显示电话号码

91

Page 92: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

dial.setData(Uri.parse("mailto:[email protected]")); dial.setData(Uri.parse("http://www.jollen.org"));

我的意图:显示电话号码

Android能看透你的意图

92

Page 93: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Intent 的 Action

每个意图(Intent)都带有一个动作(action),并根据不同的动作去行动

Intent的动作可以是自行定义与框架内部定义二种

Android框架的Intent有很多方便实用的「内建动作」

93

Page 94: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Intent 的 Data

Intent的action指定这个Intent的「动作」是什么框架依指定的动作进行处理;有些action可以附

带一笔资料

资料是以Uri的格式撰写

94

Page 95: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

更多的Intent范例

ACTION_CALL:拨号ACTION_GET_CONTENT:启动内容选取器ACTION_SET_WALLPAPER:设定Wallpaper

95

Page 96: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Activity ActionAndroid内建的Intent action分为二种:

Activity Action:启动Activity的action Broadcast Action:透过广拨器处理的action

activity action:通知框架启动Activity

96

Page 97: Android Application: Introduction

public class HelloIntentDialer extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent dial = new Intent(); dial.setAction("android.intent.action.CALL"); dial.setData(Uri.parse("tel:119")); startActivity(dial); }}

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

使用ACTION_CALL实作自动拨号

97

Page 98: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.moko.hellointentdialer" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloIntentDialer" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.permission.CALL_PHONE" /></manifest>

Page 99: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

ACTION_GET_CONTENT实作一个音乐拨放机:执行HelloIntentSelect后,出现一个「拨放音乐」的按钮按下按钮后,选择一个音乐档拨放

实作「选择音乐档」的做法是使用Intent的「chooser」观念

99

Page 100: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

HelloIntentSelect.java

100

1.建立action为ACTION_GET_CONTENT的Intent:Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

2.设定Intent的mime type,例如:设定Intent的mime type为声音档案:intent.setType("audio/*");

3.建立内容选择器并送出Intent:startActivity(Intent.createChooser(intent, "Select music"));

Page 101: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Action写法比较 属性(attribute)写法:

dial.setAction("android.intent.action.CALL");

常数写法:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

在「Intent」类别(class)里,定义了action的常数

101

Page 102: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

执行画面

Page 103: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

SET_WALLPAPER

103

public class HelloIntentWallpaper extends Activity implements View.OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.set_wallpaper); button.setOnClickListener(this); }

public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER); startActivity(Intent.createChooser(intent, "Select Wallpaper")); }}

Page 104: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

layout/main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/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="@string/hello" /> <Button android:id="@+id/set_wallpaper" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/set_wallpaper"> <requestFocus /> </Button> </LinearLayout>

Page 105: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

8.Android UI设计-UI事件处理

105

Page 106: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

什么是事件监听器(Event Listener)

UI的使用者事件处理,即View如何处理使用者

的操作,是一个重要的课题

View是重要的类别,它是与使用者互动的前线

在Android框架的设计中,以事件监听器

(event listener)的方式来处理UI的使用者事件

106

Page 107: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Event Listener 的 callback method

Android框架提供了非常良好的UI事件处理机制

先前的教学提到,View是绘制UI的类别,每个

View物件都可以向Android框架注册一个事件监听

每个事件监听器都包含一个回呼函数(callback

method),这个回呼函数(callback method)主

要的工作就是回应或处理使用者的操作

107

Page 108: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Event Listener的观念以「使用者触碰(touch)」的动作来说,当

View要处理使用者触碰的事件时,就要向

Android框架注册View.OnClickListener事件监听

当「touch」事件发生时,Android框架便回呼

事件监听器里的回呼函数

108

Page 109: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Event Listener 的原理

View.OnClickListener是click listener,故名思

意,这是UI的「Click动作监听器」

当使用者对View进行Click操作时(即触控画面

上的UI元件),Android框架便会回呼这个

View.OnClickListener的回呼函数

View.OnClickListerner的回呼函数为OnClick()

109

Page 110: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

良好的Click Listener实作

一个较为良好的实作方法是在我们的Acitivty类

别里实作View.OnClickListener介面

110

Page 111: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Event Listener 的原理每一个View都可以注册一个event listener

当Android框架收到「click」事件后,便回呼

event listener的callback method

以Button类别(按钮元件)为例,要处理使用

者触控按钮的事件时,就要呼叫Button类别的

setOnClickListener()方法来注册click listener

111

Page 112: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.btn); button.setOnClickListener(this); }

Click Listener实作范例

112

Page 113: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

public void onClick(View v) { Toast.makeText( this, "Yes.", Toast.LENGTH_LONG).show(); }

Click Listener实作:onClick()

113

Page 114: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting 114

Page 115: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

9. App Widget

115

Page 116: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

建立 Widget使用 AppWidgetProviderInfo

描述 Widget,例如layout

以 XML 方式描述

使用 AppWidgetProvider

View layout

116

Page 117: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

建立 App Widget

使用 AppWidgetProviderInfo

使用AppWidgetProvider

定义basic method

interfacing with App Widgets

当Widget更新、启动、停止或删除时会收到broadcast

View layout

117

Page 118: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

建立 Widget使用 AppWidgetProviderInfo

使用 AppWidgetProvider

View layout

定义App Widget的layout

使用 XML

118

Page 119: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

<receiver android:name="ExampleAppWidgetProvider" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info" /></receiver>

宣告 AppWidgetProvider

AndroidManifest.xml

119

Page 120: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="240dp" <!-- density-independent pixels --> android:minHeight="72dp" android:initialLayout="@layout/example_appwidget" android:configure="com.example.android.ExampleAppWidgetConfigure" ></appwidget-provider>

AppWidgetProviderInfo 的 metadata

使用<appwidget-provider>定义

存放于res/xml/资料夹

120

Page 121: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="240dp" <!-- density-independent pixels --> android:minHeight="72dp" android:updatePeriodMillis="86400000" <!-- once per day --> android:initialLayout="@layout/example_appwidget" android:configure="com.example.android.ExampleAppWidgetConfigure" ></appwidget-provider>

AppWidgetProviderInfo 的 metadata

App widget最重要的就是可以显示timely information

例如:时间、天气

121

Page 122: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Source: Android Dev Guide

使用者可以自行在桌面建立widget

122

Page 123: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

public class ExampleAppWidgetProvider extends AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length;

// Perform this loop procedure for each App Widget that belongs to this provider for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i];

// Create an Intent to launch ExampleActivity Intent intent = new Intent(context, ExampleActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

// Get the layout for the App Widget and attach an on-click listener to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout); views.setOnClickPendingIntent(R.id.button, pendingIntent);

// Tell the AppWidgetManager to perform an update on the current App Widget appWidgetManager.updateAppWidget(appWidgetId, views); } }}

App Widget实作范例

123

Page 124: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

使用 Permission

应用程式在安装时,由installer授与权限

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.app.myapp" > <uses-permission android:name="android.permission.RECEIVE_SMS" /></manifest>

124

Page 125: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Source: Android Dev Guide

标准的widget大小

125

Page 126: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

Source: Android Dev Guide

Landscape Orientation

126

Page 127: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

如何设计App Widget图档

决定widget大小

选择相匹配的frame

套用标准的阴影设计

绘制标准按纽图

调整图的尺寸大小

存档并注意存档的设定

127

Page 128: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

如何储存App Widget图档使用PNG-24格式

设定为transparent background

8-bit color

128

Page 129: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

10. Android应用UI设计考量-- Style&Theme篇

129

Page 130: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

什么是样式(Styles)

130

让应用程式里的元件(widget)「长」得跟别人很不一样

Android的styles功能,主要的对象是widget,样式是为了套用到widget上

Android还提供布景(theme)功能,可以做更大范围的套用

Page 131: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

样式的使用规定在Android专案里以XML资源档来定义「样式」

一个Android专案可以定义多个样式

让widget套用其中一个样式

131

Page 132: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

如何定义样式

132

Page 133: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

styles.xml范例

<?xml version="1.0" encoding="utf-8"?><resources> <style name="myText"> <item name="android:textSize">18sp</item> <item name="android:textColor">#880</item> </style></resources>

133

Page 134: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

如何套用样式?

134

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/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, this is HelloStyles." style="@style/myText" /></LinearLayout>

Page 135: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

执行范例

135

Page 136: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

什么是布景(theme)?布景(theme)是可以大范围套用的UI美化功能,其套用范围为「整个萤幕」

从程式码的角度来看,布景可以套用到以下二个范围:

整个应用程式(application)

整个activity

136

Page 137: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

设定无视窗标题(title)

137

<?xml version="1.0" encoding="utf-8"?><resources> <style name="myTheme"> <item name="android:windowNoTitle">true</item> </style> </resources>

Page 138: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

套用布景(theme)

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.moko.hellotheme" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/myTheme"> <activity android:name=".HelloTheme" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>

138

Page 139: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

执行范例

139

Page 140: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

使用setTheme()

140

package com.moko.hellotheme;

import android.app.Activity;import android.os.Bundle;

public class HelloTheme extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* setTheme(R.style.myTheme); */ setContentView(R.layout.main); }}

Page 141: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting 141

Page 142: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

R.anim

142

动画资源

com.examples.android.apis.view/Animation1.java

res/anim/shake.xml

<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2007 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.-->

<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="10" android:duration="1000" android:interpolator="@anim/cycle_7" />

Page 143: Android Application: Introduction

<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2007 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.-->

<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

@anim/cycle_7.xml

143

Page 144: Android Application: Introduction

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:text="@string/animation_1_instructions" /> <EditText android:id="@+id/pw" android:layout_width="fill_parent" android:layout_height="wrap_content" android:clickable="true" android:singleLine="true" android:password="true" />

<Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/googlelogin_login" />

</LinearLayout>

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

res/layout/animation_1.xml

144

Page 145: Android Application: Introduction

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.view.animation.Animation;import android.view.animation.AnimationUtils;

public class Animation1 extends Activity implements View.OnClickListener {

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.animation_1);

View loginButton = findViewById(R.id.login); loginButton.setOnClickListener(this); }

public void onClick(View v) { Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); findViewById(R.id.pw).startAnimation(shake); }

}

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting 145

Page 146: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

执行范例

146

Page 147: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting 147

Page 148: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

R.drawable

148

可支援Nine.Patch格式图档

将Nine.Patch图档做背景

Android自动做扩展

以图档里的左边线与上边线定义可扩展区、文字被置于可扩展区内

Page 149: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

tools/draw9patch

149

Page 150: Android Application: Introduction

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting 150

Page 151: Android Application: Introduction

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="small world" android:textSize="12sp" android:background="@drawable/arrow" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="big world" android:textSize="24sp" android:background="@drawable/arrow" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="super world" android:textSize="48sp" android:background="@drawable/arrow" /></LinearLayout>

《Android應用開發與底層技術》Copyright (c) 2009 Jollen’s Consulting 課程開發與提供. www.jollen.org/consulting

使用Nine.Patch图做为背景

151

Page 152: Android Application: Introduction

www.jollen.org/consulting