Voorbeeld-programma's Activity met een TextView Meerdere views stapelen Reageren op een Button Alles tegelijk Eigen tekening
(2.2) Hallo één klasse ...met één methode toekennings- opdrachten using Android.OS; using Android.App; using Android.Widget; using Android.Graphics; [ActivityAttribute(Label = "Hello", MainLauncher = true)] public class HalloApp : Activity { protected override void OnCreate(Bundle b) { base.OnCreate(b); TextView scherm; scherm = new TextView(this); scherm.Text = "Hallo!"; scherm.TextSize = 80; scherm.SetBackgroundColor(Color.Yellow); scherm.SetTextColor(Color.DarkBlue); this.SetContentView(scherm); } één klasse ...met één methode toekennings- opdrachten ...met acht opdrachten accolades begrenzen klasse en methode methode- aanroepen
(2.6) Klok AnalogClock wijzerklok; wijzerklok = new AnalogClock(this); wijzerklok.SetBackgroundColor(Color.Yellow); TextClock tekstklok; tekstklok = new TextClock(this); tekstklok.Format24Hour = "EEE HH:mm:ss"; tekstklok.TextSize = 50; TextView scherm; scherm = new TextView(this); scherm.Text = "Hallo!"; scherm.TextSize = 80; scherm.SetBackgroundColor(Color.Yellow); scherm.SetTextColor(Color.DarkBlue); this.SetContentView( scherm );
(2.6) Klok AnalogClock wijzerklok; wijzerklok = new AnalogClock(this); wijzerklok.SetBackgroundColor(Color.Yellow); TextClock tekstklok; tekstklok = new TextClock(this); tekstklok.Format24Hour = "EEE HH:mm:ss"; tekstklok.TextSize = 50; LinearLayout stapel; stapel = new LinearLayout(this); stapel.Orientation = Orientation.Vertical; stapel.AddView(wijzerklok); stapel.AddView(tekstklok); this.SetContentView( scherm ); stapel
(3.1) Teller Button public class KlikkerApp : Activity { int teller; Button knop; protected override void OnCreate(Bundle b) { base.OnCreate(b); knop = new Button(this); knop.Text = "Klik hier!"; knop.TextSize = 40; SetContentView(knop); } teller = 0; Button knop.Click += klik; public void klik(object o, EventArgs ea) { teller = teller + 1; knop.Text = teller.ToString() + " keer geklikt"; } }
Event-property van Button public class KlikkerApp : Activity { (3.1) Teller int teller; Button knop; protected override void OnCreate(Bundle b) { base.OnCreate(b); knop = new Button(this); knop.Text = "Klik hier!"; knop.TextSize = 40; SetContentView(knop); } teller = 0; properties van Button Event-property van Button knop.Click += klik; …krijgt als waarde een… public void klik(object o, EventArgs ea) { Event-handler teller = teller + 1; knop.Text = teller.ToString() + " keer geklikt"; } }
(3.1) Teller Button public class KlikkerApp : Activity { Button knop; int teller; protected override void OnCreate(Bundle b) { base.OnCreate(b); knop = new Button(this); knop.Text = "Klik hier!"; knop.TextSize = 40; knop.Click += klik; SetContentView(knop); } teller = 0; Button public void klik(object o, EventArgs ea) { teller = teller + 1; knop.Text = teller.ToString() + " keer geklikt"; } }
(3.2) Mixer SeekBar SeekBar SeekBar Button public class MixerApp : Activity { (3.2) Mixer Button knop; SeekBar rood, groen, blauw; protected override void OnCreate(Bundle b) { base.OnCreate(b); knop = new Button(this); knop.Text = "Klik hier!"; knop.TextSize = 40; knop.Click += kies; SetContentView(knop); } rood = new SeekBar(this); rood.SetBackgroundColor(Color.Red); rood.ProgressChanged += schuif; groen= new SeekBar(this); groen.SetBackgroundColor(Color.Green); groen.ProgressChanged += schuif; blauw= new SeekBar(this); blauw.SetBackgroundColor(Color.Blue); blauw.ProgressChanged += schuif; SeekBar SeekBar SeekBar Button LinearLayout lay = …… SetContentView(lay); public void kies(object o, EventArgs ea) { } public void schuif(object o, EventArgs ea) { } }
(3.2) Mixer SeekBar SeekBar SeekBar Button SeekBar rood, groen, blauw; Button knop; (3.2) Mixer public void schuif(object o, EventArgs ea) { int r, g, b; r = rood.Progress; g = groen.Progress; b = blauw.Progress; SeekBar SeekBar SeekBar knop.Text = "r:" + r + "g:" + g + "b:" + b; knop.SetBackgroundColor ( new Color(r, g, b) ); Button } public void kies(object o, EventArgs ea) { Random gen; gen = new Random(256); rood.Progress = gen.Next(); groen .Progress = gen.Next(); blauw .Progress = gen.Next(); }
(4.1) Maak je eigen View bestaat nog niet… protected override void OnCreate(Bundle b) { base.OnCreate(b); TextView t; t = new TextView(this); this.SetContentView(t); AnalogClock a; a = new AnalogClock(this); this.SetContentView(a); MondiaanView m; m = new MondriaanView(this); this.SetContentView(m); }
(4.1) Maak je eigen View public class MondriaanView : View { public MondriaanView(Context ct) { } : base(ct) base.View(ct); this.SetBackgroundColor(Color.White); protected override void OnDraw(Canvas cv) { base.OnDraw(cv); cv . DrawLine(…); cv . DrawRect(…); cv . DrawCircle(….); } }
(4.6) Maak je eigen Methodes public class MondriaanView : View { protected override void OnDraw(Canvas cv) { base.OnDraw(cv); cv . DrawLine(…); cv . DrawRect(…); cv . DrawCircle(….); this . TekenHuis(cv, …); } }
(4.6) Maak je eigen Methodes private void tekenHuis (Canvas c, … ) { Paint verf = new Paint(); int x, int y, int br) (tx,ty) int tx = x+br/2; int ty = y–br–br/2; c.DrawRect ( … , verf); c.DrawLine ( … , verf); c.DrawLine ( … , verf); x, y-br, x+br, y x, y-br, tx, ty tx, ty, x+br, y-br } (x,y) br protected override void OnDraw(Canvas c) { this . tekenHuis (c, …); this . tekenHuis (c, …); this . tekenHuis (c, …); 20, 100, 40); 70, 100, 40); 120, 100, 60); }
(3.1) Teller class MijnAct : Activity { Button knop; protected override void OnCreate(Bundle b) { base.OnCreate(b); knop = new Button(this); knop.Click += klik; this.SetContentView(knop); } public void klik(object o, EventArgs ea) { } knop.Text = "1x geklikt"; }
(6.1) Touch class MijnAct : Activity { MijnView scherm; protected override void OnCreate(Bundle b) { base.OnCreate(b); scherm = new MijnView(this); knop.Click += klik; this.SetContentView(scherm); } public void klik(object o, EventArgs ea) { } knop.Text = "1x geklikt"; }
(6.1) Touch class MijnAct : Activity { MijnView scherm; protected override void OnCreate(Bundle b) { base.OnCreate(b); scherm = new MijnView(this); scherm.Click += klik; this.SetContentView(scherm); } } class MijnView : View { public MijnView(Context c) : base(c) { } protected override void OnDraw(Canvas c) { base.OnDraw(c); } c.DrawCircle(...); }
(6.1) Touch class MijnAct : Activity { MijnView scherm; protected override void OnCreate(Bundle b) { base.OnCreate(b); scherm = new MijnView(this); scherm.Touch += raakAan; this.SetContentView(scherm); } public void raakAan(object o, EventArgs ea) { } } class MijnView : View { public MijnView(Context c) : base(c) { } protected override void OnDraw(Canvas c) { base.OnDraw(c); } c.DrawCircle(...); }
(6.1) Touch class MijnAct : Activity { MijnView scherm; protected override void OnCreate(Bundle b) { base.OnCreate(b); scherm = new MijnView(this); scherm.Touch += raakAan; this.SetContentView(scherm); } public void raakAan(object o, EventArgs ea) { } } class MijnView : View { public MijnView(Context c) : base(c) { } this.Touch += raakAan; protected override void OnDraw(Canvas c) { base.OnDraw(c); } c.DrawCircle(...); public void raakAan(object o, EventArgs ea) { } }
(6.1) Touch class MijnAct : Activity { MijnView scherm; protected override void OnCreate(Bundle b) { base.OnCreate(b); scherm = new MijnView(this); this.SetContentView(scherm); } } class MijnView : View { public MijnView(Context c) : base(c) { } this.Touch += raakAan; protected override void OnDraw(Canvas c) { base.OnDraw(c); } c.DrawCircle(...); public void raakAan(object o, EventArgs ea) { } }
(6.1) Touch } class MijnView : View { float x, y; public MijnView(Context c) : base(c) { this.Touch += raakAan; } protected override void OnDraw(Canvas c) { base.OnDraw(c); } c.DrawCircle(x, y, 50, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } x = tea.Event.GetX(); y = tea.Event.GetY(); this.Invalidate(); }
(6.1) Touch } class MijnView : View { PointF p; float x, y; public MijnView(Context c) : base(c) { this.Touch += raakAan; } protected override void OnDraw(Canvas c) { base.OnDraw(c); } c.DrawCircle(x, y, 50, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } x = tea.Event.GetX(); y = tea.Event.GetY(); this.Invalidate(); }
(6.1) Touch } class MijnView : View { PointF p; public MijnView(Context c) : base(c) { this.Touch += raakAan; } protected override void OnDraw(Canvas c) { base.OnDraw(c); } c.DrawCircle(p.X, p.Y, 50, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); this.Invalidate(); }
(6.1) Touch } class MijnView : View { PointF p; public MijnView(Context c) : base(c) { this.Touch += raakAan; } protected override void OnDraw(Canvas c) { base.OnDraw(c); } if (p != null) c.DrawCircle(p.X, p.Y, 50, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); this.Invalidate(); }
if-opdracht if (temperatuur<0) uitvoer.Text = "Het vriest!”; else uitvoer.Text = "Het dooit."; opdracht wordt alleen uitgevoerd als voorwaarde geldt opdracht wordt alleen uitgevoerd als voorwaarde niet geldt
Vergelijk-operatoren < kleiner dan <= kleiner dan of gelijk aan > groter dan >= groter dan of gelijk aan == gelijk aan != ongelijk aan x=5 x wordt 5 ! x==5 is x gelijk aan 5 ?
Syntax van opdrachten opdracht naam naam ( expressie ) ; . , expressie klasse naam methode naam ( expressie ) ; . , object expressie property naam += variabele = expressie ; return expressie ; if ) ( expressie opdracht else opdracht blok
(6.1) Touch } class MijnView : View { PointF p; public MijnView(Context c) : base(c) { this.Touch += raakAan; } protected override void OnDraw(Canvas c) { base.OnDraw(c); } if (p != null) c.DrawCircle(p.X, p.Y, 50, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); this.Invalidate(); }
(6.1) Bitmap } class MijnView : View { PointF p; Bitmap b; public MijnView(Context c) : base(c) { this.Touch += raakAan; } b = BitmapFactory.DecodeResource(...Icon...); protected override void OnDraw(Canvas c) { base.OnDraw(c); } if (p != null) c.DrawBitmap(b, p.X, p.Y, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); this.Invalidate(); }
Punten- klikker met bitmap } class MijnView : View { Bitmap b; List<PointF> alles = new List<PointF>( ) ; public MijnView(Context c) : base(c) { this.Touch += raakAan; } met bitmap b = BitmapFactory.DecodeResource(...Icon...); protected override void OnDraw(Canvas c) { base.OnDraw(c); } foreach( PointF p in alles) c.DrawBitmap(b, p.X, p.Y, new Paint()); public void raakAan(object o, TouchEventArgs tea) { } float x, y; PointF p; x = tea.Event.GetX(); y = tea.Event.GetY(); p = new PointF(x, y); alles.Add(p); this.Invalidate(); }
Syntax van opdrachten opdracht naam naam ( expressie ) ; . , expressie klasse naam methode naam ( expressie ) ; . , object expressie property naam += variabele = expressie ; return expressie ; if ) ( expressie opdracht else opdracht blok foreach ( type naam in expr ) opdracht