Vous êtes sur la page 1sur 6

Lecture 2 - Intent, onClick, Animation

< CS193A Android Programming Today: anonymous inner classes, starting intents, animation, toast notifications. Today's example code is all in an activity called Example1Activity -- the key bits of code are pasted in below.

The R.xxx space - "res"


@+id/button1 (in XML) R.id.button1 (in Java) (CT checking, autocomplete -- neat) res/drawable/abbysmile.jpg (in file system) R.drawable.abbysmile (in Java) -- uses filename (like our main.xml layout)

Add Image Resource and ImageView


Added a abbysmile.jpg in the location res/drawable/abbysmile.jpg Drag out an ImageView into the vertical layout, set it to use your image. Maybe adjust its max size. Set "src" property, knows about existing drawables Set "center horizontal" in Graphical Layout editor to center it It works in the preview .. you don't really have to run to see it.

Alternate Resources
The "drawable-hdpi" etc. folders provide alternate resources depending on runtime details -- screen size, locale, etc. For example, the selector "-hdpi" is for high pixel devices, "-es" for Spanish. Plain "drawable" folder is the base default. Your code can just refer to R.drawable.abbysmile or whatever, and the system does the logic to select the right one automatically. More on this later.

Member Vars, Anonymous Inner Classes


/ /T h i sc l a s sr e v i e w sm e m b e rv a r i a b l e sv s .l o c a lv a r i a b l e s ,a n ds h o w s / /A n o n y m o u sI n n e rC l a s s e sw h i c hm a yb en e wm a t e r i a lf o rs o m e . c l a s sA c c o u n t{

p r i v a t ei n tm B a l a n c e ; / /1 .C l a s s i cm e m b e rv a r i a b l e( a l s ok n o w na n" i n s t a n c ev a r i a b l e " ) / /C a nu s e" m "a tn a m es t a r t / /E x i s t si nt h eA c c o u n to b j e c t-o u t l i v e sa n yo n em e t h o dc a l l . p u b l i cA c c o u n t ( ){ m B a l a n c e=0 ; } p u b l i cv o i dd e p o s i t ( i n ta m t ){ S t r i n gl o g M s g=" d e p o s i t : "+a m t ; / /2 .l o g M s gi sal o c a l( s t a c k )v a r i a b l e/ /i te x i s t sa st e m p o r a r ys t o r a g ej u s tw h i l ed e p o s i t ( )i sr u n n i n g . / /C o n t r a s tt ot h em B a l a n c ev a r i a b l e . m B a l a n c e=m B a l a n c e+a m t ; } / * V a r i a b l er u l e s : U s eam e m b e rv a r i a b l ei fy o uw a n ts t o r a g ei nt h eo b j e c tt h a to u t l i v e s a n yo n em e t h o d .O f t e ns e tu pi no n C r e a t e ( ) ,m o d i f i e db ym e t h o d so v e rt i m e ( m B a l a n c ei sag o o de x a m p l eo ft h i sp a t t e r n ) . U s eal o c a lv a r i a b l et oh o l da n yt e m p o r a r yr e s u l to rp o i n t e rn e e d e d* d u r i n g * am e t h o dr u n ,b u tn o tn e e d e da f t e ri te x i t s .L o c a lv a r i a b l e sa r es i m p l e ra n d h a v eb e t t e rp e r f o r m a n c e ,s ot h e ya r eag o o dd e f a u l ts t r a t e g y . * /

/ /Ab a s i ce x a m p l eo fa nA n o n y m o u sI n n e rC l a s s . p u b l i cv o i di n n e r D e m o ( ){ f i n a li n tt e m p=m B a l a n c e/1 0 ; R u n n a b l er u n n a b l e= / /T h i si sa n" a n o n y m o u si n n e rc l a s s "-3p a r t s n e wR u n n a b l e ( ){ / /1 .T h es u p e r c l a s s p u b l i cv o i dr u n ( ){ / /2 .m e t h o d ( s )i n s i d et h eA I C / /3 .C o d ei n s i d et h em e t h o d-t y p i c a l l yt h i si st h ep a r ty o uc a r e / /a b o u t .S e en o t e sb e l o w S y s t e m . o u t . p r i n t l n ( " r u n ( )i sr u n n i n g " ) ;

d e p o s i t ( t e m p ) ; / /C a nc a l lm e t h o d so nt h eo u t e ro b j e c t } } ; / * C o d ei n s i d ea nA I Cm e t h o d : 1 .T h eA I Cc r e a t e sas e p a r a t eo b j e c tw i t hi t so w nm e t h o d s . H o w e v e r ,t h eA I Ck e e p sap o i n t e rt oi t so w n i n g ,o u t e ro b j e c tt or e f e r t oi t sv a r i a b l e sa n dc a l li t sm e t h o d st o o( e . g .d e p o s i t ( ) ) . 2 ." t h i s "h e r er e f e r st ot h eA I Ci t s e l f .U s et h es y n t a x" O u t e r . t h i s "t og e t ap o i n t e rt ot h eo u t e ro b j e c t . 3 .C a n n o tr e f e rt ol o c a lv a r i a b l e sw h e r et h eA I Cw a sc r e a t e d( e . g .t e m p ) u n l e s st h e ya r ed e c l a r e d" f i n a l " . F u nF a c t :i nJ a v a8 ,A I C ' sa r eb e i n gr e p l a c e dw i t ham u c hs i m p l e r" l a m b d a " s y n t a x-y a y ! * / } }

Button OnClickListener
Last bit from previous lecture -- OnClickListener is an AIC.
p u b l i cv o i do n C r e a t e ( B u n d l es a v e d I n s t a n c e S t a t e ){ s u p e r . o n C r e a t e ( s a v e d I n s t a n c e S t a t e ) ; s e t C o n t e n t V i e w ( R . l a y o u t . m a i n ) ; / /S t o r eap o i n t e rt oe d i t T e x t 1 .S e tu pb u t t o n 1t oa d da! . e d i t T e x t 1=( E d i t T e x t )f i n d V i e w B y I d ( R . i d . e d i t T e x t 1 ) ; B u t t o nb u t t o n=( B u t t o n )f i n d V i e w B y I d ( R . i d . b u t t o n 1 ) ; b u t t o n . s e t O n C l i c k L i s t e n e r ( / /C r e a t eaO n C l i c k L i s t e n e rA I C n e wO n C l i c k L i s t e n e r ( ){ p u b l i cv o i do n C l i c k ( V i e wv ){ e d i t T e x t 1 . s e t T e x t ( e d i t T e x t 1 . g e t T e x t ( )+" ! " ) ; } } ) ; . . .

Intents
Intents -- the mechanism by which various activities/components invoke each other. This is just a first, simple example. Intent has an action and data. Here we specify the "open url" intent .. which will launch the appropriate new activity for that. The "back button" from there comes back to us.

Intent Example -- Open Url


/ /B u t t o n 2 :s t a r tw e bi n t e n t e d i t T e x t 2=( E d i t T e x t )f i n d V i e w B y I d ( R . i d . e d i t T e x t 2 ) ; B u t t o nb u t t o n 2=( B u t t o n )f i n d V i e w B y I d ( R . i d . b u t t o n 2 ) ; b u t t o n 2 . s e t O n C l i c k L i s t e n e r ( n e wO n C l i c k L i s t e n e r ( ){ p u b l i cv o i do n C l i c k ( V i e wv ){ I n t e n ti n t e n t=n e wI n t e n t ( I n t e n t . A C T I O N _ V I E W ) ;/ /m o s tc o m m o n i n t e n t . s e t D a t a ( U r i . p a r s e ( e d i t T e x t 2 . g e t T e x t ( ) . t o S t r i n g ( ) ) ) ; s t a r t A c t i v i t y ( i n t e n t ) ; / /N o t e :t h i si saE x a m p l e A c t i v i t ym e t h o d / * / /A l t e r n a t e l y ,s t a r t i n gad i a li n t e n t . I n t e n ti n t e n t=n e wI n t e n t ( I n t e n t . A C T I O N _ D I A L ) ;/ /v s ._ C A L L i n t e n t . s e t D a t a ( U r i . p a r s e ( " t e l : "+e d i t T e x t 2 . g e t T e x t ( ) . t o S t r i n g ( ) ) ) ; / /u r if o r ms h o u l db e" t e l : 5 5 5 5 5 5 5 5 5 5 " * / } } ) ;

Complaint Button
Write as a separate doComplain() method, call it from a one line AIC. Write the animation in a separate res/anim/swirl.xml file
p u b l i cv o i dd o C o m p l a i n ( ){ A n i m a t i o na n i m a t i o n=A n i m a t i o n U t i l s . l o a d A n i m a t i o n ( t h i s ,R . a n i m . s w i r l ) ; / /T h i sw h o l em i d d l ep i e c ei so p t i o n a l

a n i m a t i o n . s e t A n i m a t i o n L i s t e n e r ( n e wA n i m a t i o n L i s t e n e r ( ){ p u b l i cv o i do n A n i m a t i o n S t a r t ( A n i m a t i o na n i m ) { } ; p u b l i cv o i do n A n i m a t i o n R e p e a t ( A n i m a t i o na n i m ) { } ; p u b l i cv o i do n A n i m a t i o n E n d ( A n i m a t i o na n i m ) { e d i t T e x t 3 . s e t T e x t ( " " ) ; C o n t e x tc o n t e x t=g e t A p p l i c a t i o n C o n t e x t ( ) ; C h a r S e q u e n c et e x t=" Y o u rf e e d b a c kh a sb e e nf i l e da p p r o p r i a t e l y ! " ; i n td u r a t i o n=T o a s t . L E N G T H _ S H O R T ; T o a s tt o a s t=T o a s t . m a k e T e x t ( c o n t e x t ,t e x t ,d u r a t i o n ) ; t o a s t . s h o w ( ) ; } ; } ) ;

e d i t T e x t 3 . s t a r t A n i m a t i o n ( a n i m a t i o n ) ; }

Animation XML
< < ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " u t f 8 " ? > < s e tx m l n s : a n d r o i d = " h t t p : / / s c h e m a s . a n d r o i d . c o m / a p k / r e s / a n d r o i d " a n d r o i d : s h a r e I n t e r p o l a t o r = " f a l s e " > < ! - t h i si si nr e s / a n i m / s w i r l . x m l> < ! - m a k ei ts m a l l e rf o r8 0 0 m s> < s c a l e a n d r o i d : i n t e r p o l a t o r = " @ a n d r o i d : a n i m / a c c e l e r a t e _ d e c e l e r a t e _ i n t e r p o l a t o r " a n d r o i d : f r o m X S c a l e = " 1 . 0 " a n d r o i d : t o X S c a l e = " 0 . 5 " a n d r o i d : f r o m Y S c a l e = " 1 . 0 " a n d r o i d : t o Y S c a l e = " 0 . 1 " a n d r o i d : p i v o t X = " 5 0 % "

a n d r o i d : p i v o t Y = " 5 0 % " a n d r o i d : f i l l A f t e r = " f a l s e " a n d r o i d : d u r a t i o n = " 8 0 0 "/ > < ! - T h e nr o t a t ea n df a d eo u t( a l p h a=0 )f o r8 0 0m s> < r o t a t e a n d r o i d : f r o m D e g r e e s = " 0 " a n d r o i d : t o D e g r e e s = " 7 2 0 " a n d r o i d : p i v o t X = " 5 0 % " a n d r o i d : p i v o t Y = " 5 0 % " a n d r o i d : s t a r t O f f s e t = " 8 0 0 " a n d r o i d : d u r a t i o n = " 8 0 0 "/ > < a l p h a a n d r o i d : f r o m A l p h a = " 1 . 0 " a n d r o i d : t o A l p h a = " 0 . 0 " a n d r o i d : s t a r t O f f s e t = " 8 0 0 " a n d r o i d : d u r a t i o n = " 8 0 0 "/ > < / s e t >

AnimationListener, Toast Alert


Previous code example: use AnimationListener AIC to listen for end of animation. At that moment, change text, put up standard "toast" alert.

Homework 2
Add to your homework1 app -- you don't need to turn this in, it's just for practice. Start at least one intent based on a user action (web or otherwise) Add an animation to some widget on screen Use AnimationListener to do something on the start or end of the animation

Vous aimerez peut-être aussi