Android EventHandling

download Android EventHandling

of 17

Transcript of Android EventHandling

  • 8/10/2019 Android EventHandling

    1/17

    ANDROID EVENT

    HANDLING

    L. Grewe

  • 8/10/2019 Android EventHandling

    2/17

    Widget : Views that have

    events For a ist o! the widgets "rovided #$ Android%see the android.widget "a&'age.

    (o)e E*a)"es

    +,tton -he&'+o* Datei&'er EditTe*t I)ageView (ear&hView ("inner

  • 8/10/2019 Android EventHandling

    3/17

    Event Handing

    De&ide what Widgets who/s events to "ro&ess Defne an event listener and register it

    with the View.

    View.On-i&'Listener 0!or handing 1&i&'s1 on aView2% View.OnTo,&hListener 0!or handing to,&hs&reen events in a View2% andView.On3e$Listener 0!or handing devi&e 'e$"resses within a View2

    http://developer.android.com/guide/topics/ui/ui-events.html details more

  • 8/10/2019 Android EventHandling

    4/17

    4sing E&i"se 55ets re&a o,r e*a)"e!ro) o,r starting e&t,re

    6Heo Word ro7e&t8 Lets add a #,tton

  • 8/10/2019 Android EventHandling

    5/17

    Lets add a +,tton to a

    "rogra)5#ased inter!a&e (te" 9: Add #,tton (te" : Register Event Hander

    TWO OTION( se"arate &ass to hande

    event0s2% OR have the A&tivit$ &ontaining the#,tton do the event handing

    (te" ;: I)"e)ent Event Hander

  • 8/10/2019 Android EventHandling

    6/17

    Event handing done #$

    A&tivit$ itse! Here &ode to hande is inside A&tivit$ itse! ",#i& &ass E*a)"eA&tivit$ e*tends A&tivit$ i)"e)ents On-i&'Listener=

    "rote&ted void on-reate0+,nde savedVa,es2 =

    ...

    +,tton #,tton ? 0+,tton2@ndView+$Id0R.id.&or'$2 //STEP 1 #,tton.setOn-i&'Listener0this2 //STEP - registration B

    CC I)"e)ent the On-i&'Listener &a#a&' //STEP ! "event handler ",#i& void on-i&'0View v2 =

    CC do so)ething when the #,tton is &i&'ed B ...B

  • 8/10/2019 Android EventHandling

    7/17

    Event Handing 5 here have

    a (EARATE &assEVE#T $%#D&'#( )*DE in separate o+,ect m)or&istner CC -reate an anon$)o,s i)"e)entation o! On-i&'Listener STEP !

    "rivate On-i&'Listener )-or'$Listener ? new On-i&'Listener02 = ",#i& void on-i&'0View v2 = CC do so)ething when the #,tton is &i&'ed

    BB

    CCNow inside $o,r A&tivit$ &ass"rote&ted void on-reate0+,nde savedVa,es2 = ...

    // STEP 1: -a"t,re o,r #,tton !ro) a$o,t +,tton #,tton ? 0+,tton2@ndView+$Id0R.id.&or'$2 // STEP : Register the on-i&' istener with the i)"e)entation a#ove #,tton.setOn-i&'Listener0)-or'$Listener2 ...B

  • 8/10/2019 Android EventHandling

    8/17

    De"ends on IDE

  • 8/10/2019 Android EventHandling

    9/17

    (TE9: -reate a +,tton

    -reate #,tton,sing L G4Iinter!a&e in E&i"se

    (et "ro"ert$ soa#e on #,tton is6Hit e8

    Aso% added te*tview withinstr,&tions teing,ser to hit #,tton

  • 8/10/2019 Android EventHandling

    10/17

    (TE: Register the )ainA""i&ation A&tivit$ as

    EventHander !or #,tton Gra# #,tton asso&iated with its id% store ino&a varia#e #.

    -a seton-i&'Listener0this2pu+lic class %ndroidEample0Event$adling%ctivit etends %ctivit implements *n)lic&istener

    int hits 234

    C -aed when the a&tivit$ is @rst &reated. C

    Override

    pu+lic void on)reate56undle saved'nstanceState7

    super.on)reate5saved'nstanceState74

    set-ontentView0R.a$o,t.main);

    +,tton # ? 0+,tton2@ndView+$Id0R.id.button1);

    #.setOn-i&'Listener0this74

    B

  • 8/10/2019 Android EventHandling

    11/17

    (TE: Register the )ainA""i&ation A&tivit$ as

    EventHander !or #,tton Have the )ain &ass 6AndroidE*a)"eeventHandingA&tivit$8i)"e)ent On-i&'Listener !or #,tton

    In )ethod on-i&' in&re)ent hits and ,se in dis"a$ string.pu+lic class %ndroidEample0Event$adling%ctivit etends %ctivit implements *n)lic&istener

    int hits 234

    C -aed when the a&tivit$ is @rst &reated. C

    Override

    pu+lic void on)reate56undle saved'nstanceState7

    //see previous slide88..

    B

    CC I)"e)ent the On-i&'Listener &a#a&'

    CC(TE ; event hander

    pu+lic void on)lic5View v7

    CC do so)ething when the #,tton is &i&'ed

    this.hits994

    Te*tView te*t ? 0Te*tView2@ndView+$Id0R.id.textView1);

    (tring s ? 1Jo, hit )e 1 K this.hits4

    te*t.setTe*t0s2

    B

    B

  • 8/10/2019 Android EventHandling

    12/17

    Lets r,n the "revio,s &ode.

    R,n $o,r &ode

  • 8/10/2019 Android EventHandling

    13/17

    A!ter r,nning it#e!ore hit

    #,tton

  • 8/10/2019 Android EventHandling

    14/17

    A!ter hitting #,tton a !ew

    ti)es

  • 8/10/2019 Android EventHandling

    15/17

  • 8/10/2019 Android EventHandling

    16/17

    Re)e)#er

  • 8/10/2019 Android EventHandling

    17/17