Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas

Aula
2
–
Views
Model‐View‐Controller
• Mode, Notas de estudo de Engenharia de Telecomunicações

Programação, Android

Tipologia: Notas de estudo

2017

Compartilhado em 31/10/2017

bruno-z36
bruno-z36 🇧🇷

4

(7)

54 documentos

1 / 40

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
Aula%2%–%Views%
O%que%é%modelo,%visão%e%controle%
Como%definir%layouts%gráficos%
Como%Separar%visão%de%modelo%
Como%criar%layouts%em%arquivos%XML%
Como%adicionar%eventos%à%aplicação%
O%padrão%observer%
Programação%para%a%Plataforma%Android%–%Aula%2%
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Pré-visualização parcial do texto

Baixe Aula
2
–
Views
Model‐View‐Controller
• Mode e outras Notas de estudo em PDF para Engenharia de Telecomunicações, somente na Docsity!

Aula 2 – Views

  • O que é modelo, visão e controle
  • Como definir layouts gráficos
  • Como Separar visão de modelo
  • Como criar layouts em arquivos XML
  • Como adicionar eventos à aplicação
  • O padrão observer

Programação para a Plataforma Android – Aula 2

Model‐View‐Controller

• Model : banco de músicas + código para tocá‐

las, lista de contatos + código de chamada,

etc.

• View : o conjunto de interfaces gráficas que

compõem uma aplicação.

• Controller : o código que faz a ligação entre a

visão e o modelo.

– Tratamento de eventos.

Exemplo

public final void onCreate(final Bundle state) { super.onCreate(state); LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams. FILL_PARENT, ViewGroup.LayoutParams. WRAP_CONTENT, 0.0F); LinearLayout.LayoutParams widgetParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams. FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, 1.0F); LinearLayout root = new LinearLayout(this); root.setOrientaaon(LinearLayout. VERTICAL); root.setBackgroundColor(Color. LTGRAY); root.setLayoutParams(containerParams); LinearLayout ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout. HORIZONTAL); ll.setBackgroundColor(Color. GRAY); ll.setLayoutParams(containerParams); root.addView(ll); EditText tb = new EditText(this); tb.setText(R.string. defaultLeJText); tb.setFocusable( false); tb.setLayoutParams(widgetParams); ll.addView(tb); tb = new EditText(this); tb.setText(R.string. defaultRightText); tb.setFocusable( false); tb.setLayoutParams(widgetParams); ll.addView(tb); ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout. HORIZONTAL); ll.setBackgroundColor(Color. DKGRAY); ll.setLayoutParams(containerParams); root.addView(ll); Bufon b = new BuCon(this); b.setText(R.string. labelRed); b.setTextColor(Color. RED); b.setLayoutParams(widgetParams); ll.addView(b); b = new BuCon(this); b.setText(R.string. labelGreen); b.setTextColor(Color. GREEN); b.setLayoutParams(widgetParams); ll.addView(b); setContentView(root); } }

Resources

public final void onCreate(final Bundle state) { super.onCreate(state); LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F); LinearLayout.LayoutParams widgetParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, 1.0F); LinearLayout root = new LinearLayout(this); root.setOrientaaon(LinearLayout.VERTICAL); root.setBackgroundColor(Color.LTGRAY); root.setLayoutParams(containerParams); LinearLayout ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout.HORIZONTAL); ll.setBackgroundColor(Color.GRAY); ll.setLayoutParams(containerParams); root.addView(ll); EditText tb = new EditText(this); tb.setText( R.string.defaultLeFText ); tb.setFocusable(false); tb.setLayoutParams(widgetParams); ll.addView(tb); tb = new EditText(this); tb.setText( R.string.defaultRightText ); tb.setFocusable(false); tb.setLayoutParams(widgetParams); ll.addView(tb); ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout.HORIZONTAL); ll.setBackgroundColor(Color.DKGRAY); ll.setLayoutParams(containerParams); root.addView(ll); Bufon b = new Bufon(this); b.setText( R.string.labelRed ); b.setTextColor(Color.RED); b.setLayoutParams(widgetParams); ll.addView(b); b = new Bufon(this); b.setText( R.string.labelGreen ); b.setTextColor(Color.GREEN); b.setLayoutParams(widgetParams); ll.addView(b); setContentView(root); } }

AulaXY1

Hello world!

Seungs

MainAcavity

X coord

Y coord

red

green

Check

strings.xml

public final void onCreate(final Bundle state) { super.onCreate(state); LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams. FILL_PARENT, ViewGroup.LayoutParams. WRAP_CONTENT, 0.0F); LinearLayout.LayoutParams widgetParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams. FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, 1.0F); LinearLayout root = new LinearLayout(this); root.setOrientaaon(LinearLayout. VERTICAL); root.setBackgroundColor(Color. LTGRAY); root.setLayoutParams(containerParams); LinearLayout ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout. HORIZONTAL); ll.setBackgroundColor(Color. GRAY); ll.setLayoutParams(containerParams); root.addView(ll); EditText tb = new EditText(this); tb.setText(R.string. defaultLeJText); tb.setFocusable( false); tb.setLayoutParams(widgetParams); ll.addView(tb); tb = new EditText(this); tb.setText(R.string. defaultRightText); tb.setFocusable( false); tb.setLayoutParams(widgetParams); ll.addView(tb); ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout. HORIZONTAL); ll.setBackgroundColor(Color. DKGRAY); ll.setLayoutParams(containerParams); root.addView(ll); Bufon b = new BuCon(this); b.setText(R.string. labelRed); b.setTextColor(Color. RED); b.setLayoutParams(widgetParams); ll.addView(b); b = new BuCon(this); b.setText(R.string. labelGreen); b.setTextColor(Color. GREEN); b.setLayoutParams(widgetParams); ll.addView(b); setContentView(root); } }

LinearLayout ll = new LinearLayout(this);

ll.setOrientation(LinearLayout. HORIZONTAL);

ll.setBackgroundColor(Color. GRAY);

ll.setLayoutParams(containerParams);

root.addView(ll);

E se eu mudasse isto! para GREEN?!

public final void onCreate(final Bundle state) { super.onCreate(state); LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams. FILL_PARENT, ViewGroup.LayoutParams. WRAP_CONTENT, 0.0F); LinearLayout.LayoutParams widgetParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams. FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, 1.0F); LinearLayout root = new LinearLayout(this); root.setOrientaaon(LinearLayout. VERTICAL); root.setBackgroundColor(Color. LTGRAY); root.setLayoutParams(containerParams); LinearLayout ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout. HORIZONTAL); ll.setBackgroundColor(Color. GRAY); ll.setLayoutParams(containerParams); root.addView(ll); EditText tb = new EditText(this); tb.setText(R.string. defaultLeJText); tb.setFocusable( false); tb.setLayoutParams(widgetParams); ll.addView(tb); tb = new EditText(this); tb.setText(R.string. defaultRightText); tb.setFocusable( false); tb.setLayoutParams(widgetParams); ll.addView(tb); ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout. HORIZONTAL); ll.setBackgroundColor(Color. DKGRAY); ll.setLayoutParams(containerParams); root.addView(ll); Bufon b = new BuCon(this); b.setText(R.string. labelRed); b.setTextColor(Color. RED); b.setLayoutParams(widgetParams); ll.addView(b); b = new BuCon(this); b.setText(R.string. labelGreen); b.setTextColor(Color. GREEN); b.setLayoutParams(widgetParams); ll.addView(b); setContentView(root); } }

LinearLayout ll = new LinearLayout(this);

ll.setOrientation(LinearLayout. HORIZONTAL);

ll.setBackgroundColor(Color. GREEN );

ll.setLayoutParams(containerParams);

root.addView(ll);

LinearLayout ll = new LinearLayout(this);

EditText tb = new EditText(this);

tb.setText(R.string.defaultLeftText);

tb.setFocusable(false);

tb.setLayoutParams(widgetParams);

ll.addView(tb);

tb = new EditText(this);

tb.setText(R.string.defaultRightText);

tb.setFocusable(false);

tb.setLayoutParams(widgetParams);

public final void onCreate(final Bundle state) { super.onCreate(state); LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams. FILL_PARENT, ViewGroup.LayoutParams. WRAP_CONTENT, 0.0F); LinearLayout.LayoutParams widgetParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams. FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, 1.0F); LinearLayout root = new LinearLayout(this); root.setOrientaaon(LinearLayout. VERTICAL); root.setBackgroundColor(Color. LTGRAY); root.setLayoutParams(containerParams); LinearLayout ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout. HORIZONTAL); ll.setBackgroundColor(Color. GRAY); ll.setLayoutParams(containerParams); root.addView(ll); EditText tb = new EditText(this); tb.setText(R.string. defaultLeJText); tb.setFocusable( false); tb.setLayoutParams(widgetParams); ll.addView(tb); tb = new EditText(this); tb.setText(R.string. defaultRightText); tb.setFocusable( false); tb.setLayoutParams(widgetParams); ll.addView(tb); ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout. HORIZONTAL); ll.setBackgroundColor(Color. DKGRAY); ll.setLayoutParams(containerParams); root.addView(ll); Bufon b = new BuCon(this); b.setText(R.string. labelRed); b.setTextColor(Color. RED); b.setLayoutParams(widgetParams); ll.addView(b); b = new BuCon(this); b.setText(R.string. labelGreen); b.setTextColor(Color. GREEN); b.setLayoutParams(widgetParams); ll.addView(b); setContentView(root); } }

ll = new LinearLayout(this);

ll.setOrientation(LinearLayout. HORIZONTAL );

ll.setBackgroundColor(Color.DKGRAY);

ll.setLayoutParams(containerParams);

root.addView(ll);

Button b = new Button(this);

b.setText(R.string.labelRed);

b.setTextColor(Color.RED);

b.setLayoutParams(widgetParams);

ll.addView(b);

b = new Button(this);

b.setText(R.string.labelGreen);

b.setTextColor(Color.GREEN);

b.setLayoutParams(widgetParams);ll.addView(b);

ll.addView(b);

public final void onCreate(final Bundle state) { super.onCreate(state); LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams. FILL_PARENT, ViewGroup.LayoutParams. WRAP_CONTENT, 0.0F); LinearLayout.LayoutParams widgetParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams. FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, 1.0F); LinearLayout root = new LinearLayout(this); root.setOrientaaon(LinearLayout. VERTICAL); root.setBackgroundColor(Color. LTGRAY); root.setLayoutParams(containerParams); LinearLayout ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout. HORIZONTAL); ll.setBackgroundColor(Color. GRAY); ll.setLayoutParams(containerParams); root.addView(ll); EditText tb = new EditText(this); tb.setText(R.string. defaultLeJText); tb.setFocusable( false); tb.setLayoutParams(widgetParams); ll.addView(tb); tb = new EditText(this); tb.setText(R.string. defaultRightText); tb.setFocusable( false); tb.setLayoutParams(widgetParams); ll.addView(tb); ll = new LinearLayout(this); ll.setOrientaaon(LinearLayout. HORIZONTAL); ll.setBackgroundColor(Color. DKGRAY); ll.setLayoutParams(containerParams); root.addView(ll); Bufon b = new BuCon(this); b.setText(R.string. labelRed); b.setTextColor(Color. RED); b.setLayoutParams(widgetParams); ll.addView(b); b = new BuCon(this); b.setText(R.string. labelGreen); b.setTextColor(Color. GREEN); b.setLayoutParams(widgetParams); ll.addView(b); setContentView(root); } } E se fosse! vertical?!

Discussão

• Alguém aqui já programou interfaces gráficas

dessa forma?

• Quais as desvantagens desse apo de

abordagem?

• E quais seriam as alternaavas?

Árvores de Componentes

LinearLayout

(veracal)

LinearLayout

(Horizontal)

LinearLayout

(Horizontal)

Text Text Bufon Bufon

Composite:

Visões

são

agrupadas

segundo

um^ padrão

de

projetos

chamado

composite

. Alguém

já^ ouviu

falar^ disso?

Padrões de projeto:

O que é isso mesmo?

Composite

• A idéia desse padrão de projetos é tratar

objetos compostos e objetos primiavos da

mesma forma.

No caso

de

Android,

quais

classes

fazem

o

papel de Leaf

e

Composite?

Separando Visão e Modelo

• Android permite que

interfaces gráficas sejam

projetadas em XML

– XML permite que árvores

de componentes gráficos

sejam construídas com

facilidade.

– O Android SDK

disponibiliza um editor

de interfaces gráficas.

Ligando os Fios

package com.aula2_xml;

import android.app.Activity;

import android.os.Bundle;

public class Aula2_XML_Activity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

• A separação entre visão e modelo

permite que o código de modelo

fique menor e mais claro.

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

android:id= "@+id/root"

android:orientaaon= "verZcal"

android:layout_width= "fill_parent"

android:layout_height= "wrap_content">

<LinearLayout

android:orientaJon= "horizontal"

android:layout_width= "fill_parent"

android:layout_height= "wrap_content">

<EditText

android:id= "@+id/text1"

android:text= "@string/defaultLe>Text"

android:focusable= "false"

android:layout_width= "fill_parent"

android:layout_height= "fill_parent"

android:layout_weight= "1"/>

<EditText

android:id= "@+id/text2"

android:text= "@string/defaultRightText"

android:focusable= "false"

android:layout_width= "fill_parent"

android:layout_height= "fill_parent"

android:layout_weight= "1"/>

<LinearLayout

android:orientaaon= "horizontal"

android:layout_width= "fill_parent"

android:layout_height= "wrap_content">

<Bufon

android:id= "@+id/buRon1"

android:text= "@string/labelRed"

android:layout_width= "fill_parent"

android:layout_height= "fill_parent"

android:layout_weight= "1"/>

<Bufon

android:id= "@+id/buRon2"

android:text= "@string/labelGreen"

android:layout_width= "fill_parent"

android:layout_height= "fill_parent"

android:layout_weight= "1"/>

<LinearLayout

android:orientation= "horizontal"

android:layout_width= "fill_parent"

android:layout_height= "wrap_content">

<EditText

android:id= "@+id/text1"

android:text= "@string/defaultLeftText"

android:focusable= "false"

android:layout_width= "fill_parent"

android:layout_height= "fill_parent"

android:layout_weight= "1" />

<EditText

android:id= "@+id/text2"

android:text= "@string/defaultRightText"

android:focusable= "false"

android:layout_width= "fill_parent"

android:layout_height= "fill_parent"

android:layout_weight= "1"/>

E se fosse 1.5?!