Você está na página 1de 40

ProgramaoparaaPlataformaAndroidAula2

Aula2Views
Oquemodelo,visoecontrole
Comodenirlayoutsgrcos
ComoSepararvisodemodelo
ComocriarlayoutsemarquivosXML
Comoadicionareventosaplicao
Opadroobserver

ModelViewController
Model:bancodemsicas+cdigoparatoc
las,listadecontatos+cdigodechamada,
etc.
View:oconjuntodeinterfacesgrcasque
compemumaaplicao.
Controller:ocdigoquefazaligaoentrea
visoeomodelo.
Tratamentodeeventos.

CriandoInterfacesdeUsurio
Hduasestratgiasbsicasparacriarseuma
visoparaousurio:
1. ProgramaodocdigodainterfaceemJava,
diretamentenaaplicao.
2. CriaodeumarquivoXMLquedescrevea
interfacegrca.

Ambasasabordagenspossuemvantagens.
Vamosdarumaolhadanaabordagem1primeiro.

publicnalvoidonCreate(nalBundlestate){
super.onCreate(state);
LinearLayout.LayoutParamscontainerParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,0.0F);
LinearLayout.LayoutParamswidgetParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,
1.0F);
LinearLayoutroot=newLinearLayout(this);
root.setOrientaaon(LinearLayout.VERTICAL);
root.setBackgroundColor(Color.LTGRAY);
root.setLayoutParams(containerParams);
LinearLayoutll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.GRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
EditTexttb=newEditText(this);
tb.setText(R.string.defaultLeJText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
tb=newEditText(this);
tb.setText(R.string.defaultRightText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
ll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.DKGRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
Bufonb=newBuCon(this);
b.setText(R.string.labelRed);
b.setTextColor(Color.RED);
b.setLayoutParams(widgetParams);
ll.addView(b);
b=newBuCon(this);
b.setText(R.string.labelGreen);
b.setTextColor(Color.GREEN);
b.setLayoutParams(widgetParams);
ll.addView(b);
setContentView(root);
}
}

Exemplo

publicnalvoidonCreate(nalBundlestate){
super.onCreate(state);
LinearLayout.LayoutParamscontainerParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,0.0F);
LinearLayout.LayoutParamswidgetParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,
1.0F);
LinearLayoutroot=newLinearLayout(this);
root.setOrientaaon(LinearLayout.VERTICAL);
root.setBackgroundColor(Color.LTGRAY);
root.setLayoutParams(containerParams);
LinearLayoutll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.GRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
EditTexttb=newEditText(this);
tb.setText(R.string.defaultLeFText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
tb=newEditText(this);
tb.setText(R.string.defaultRightText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
ll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.DKGRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
Bufonb=newBufon(this);
b.setText(R.string.labelRed);
b.setTextColor(Color.RED);
b.setLayoutParams(widgetParams);
ll.addView(b);
b=newBufon(this);
b.setText(R.string.labelGreen);
b.setTextColor(Color.GREEN);
b.setLayoutParams(widgetParams);
ll.addView(b);
setContentView(root);
}
}

Resources

strings.xml
<resources>
<stringname="app_name">AulaXY1</string>
<stringname="hello_world">Helloworld!</string>
<stringname="menu_seungs">Seungs</string>
<stringname="atle_acavity_main">MainAcavity</string>
<stringname="defaultLevText">Xcoord</string>
<stringname="defaultRightText">Ycoord</string>
<stringname="labelRed">red</string>
<stringname="labelGreen">green</string>
<stringname="labelAnagram">Check</string>
</resources>

publicnalvoidonCreate(nalBundlestate){
super.onCreate(state);
LinearLayout.LayoutParamscontainerParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,0.0F);
LinearLayout.LayoutParamswidgetParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,
1.0F);
LinearLayoutroot=newLinearLayout(this);
root.setOrientaaon(LinearLayout.VERTICAL);
root.setBackgroundColor(Color.LTGRAY);
root.setLayoutParams(containerParams);
LinearLayoutll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.GRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
EditTexttb=newEditText(this);
tb.setText(R.string.defaultLeJText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
tb=newEditText(this);
tb.setText(R.string.defaultRightText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
ll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.DKGRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
Bufonb=newBuCon(this);
b.setText(R.string.labelRed);
b.setTextColor(Color.RED);
b.setLayoutParams(widgetParams);
ll.addView(b);
b=newBuCon(this);
b.setText(R.string.labelGreen);
b.setTextColor(Color.GREEN);
b.setLayoutParams(widgetParams);
ll.addView(b);
setContentView(root);
}
}

E o que querem dizer essas!


linhas de cdigo?!

LinearLayout.LayoutParams containerParams
= new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0.0F);
LinearLayout root = new LinearLayout(this);
root.setOrientation(LinearLayout.VERTICAL);
root.setBackgroundColor(Color.LTGRAY);
root.setLayoutParams(containerParams);
...
root.addView(ll);
...
root.addView(ll);
iz:
a
r
l
e
n
i
a
p
O
...
serve
z
i
a
r
l
e
n
i
a
setContentView(root); O p
ra os

a
de suporte p
tes.
n
e
n
o
p
m
o
c
outros

publicnalvoidonCreate(nalBundlestate){
super.onCreate(state);
LinearLayout.LayoutParamscontainerParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,0.0F);
LinearLayout.LayoutParamswidgetParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,
1.0F);
LinearLayoutroot=newLinearLayout(this);
root.setOrientaaon(LinearLayout.VERTICAL);
root.setBackgroundColor(Color.LTGRAY);
root.setLayoutParams(containerParams);
LinearLayoutll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.GRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
EditTexttb=newEditText(this);
tb.setText(R.string.defaultLeJText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
tb=newEditText(this);
tb.setText(R.string.defaultRightText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
ll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.DKGRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
Bufonb=newBuCon(this);
b.setText(R.string.labelRed);
b.setTextColor(Color.RED);
b.setLayoutParams(widgetParams);
ll.addView(b);
b=newBuCon(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?!

publicnalvoidonCreate(nalBundlestate){
super.onCreate(state);
LinearLayout.LayoutParamscontainerParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,0.0F);
LinearLayout.LayoutParamswidgetParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,
1.0F);
LinearLayoutroot=newLinearLayout(this);
root.setOrientaaon(LinearLayout.VERTICAL);
root.setBackgroundColor(Color.LTGRAY);
root.setLayoutParams(containerParams);
LinearLayoutll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.GRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
EditTexttb=newEditText(this);
tb.setText(R.string.defaultLeJText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
tb=newEditText(this);
tb.setText(R.string.defaultRightText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
ll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.DKGRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
Bufonb=newBuCon(this);
b.setText(R.string.labelRed);
b.setTextColor(Color.RED);
b.setLayoutParams(widgetParams);
ll.addView(b);
b=newBuCon(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);

publicnalvoidonCreate(nalBundlestate){
super.onCreate(state);
LinearLayout.LayoutParamscontainerParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,0.0F);
LinearLayout.LayoutParamswidgetParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,
1.0F);
LinearLayoutroot=newLinearLayout(this);
root.setOrientaaon(LinearLayout.VERTICAL);
root.setBackgroundColor(Color.LTGRAY);
root.setLayoutParams(containerParams);
LinearLayoutll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.GRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
EditTexttb=newEditText(this);
tb.setText(R.string.defaultLeJText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
tb=newEditText(this);
tb.setText(R.string.defaultRightText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
ll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.DKGRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
Bufonb=newBuCon(this);
b.setText(R.string.labelRed);
b.setTextColor(Color.RED);
b.setLayoutParams(widgetParams);
ll.addView(b);
b=newBuCon(this);
b.setText(R.string.labelGreen);
b.setTextColor(Color.GREEN);
b.setLayoutParams(widgetParams);
ll.addView(b);
setContentView(root);
}
}

E se eu removesse!
esta linha?!

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);
ll.addView(tb);

publicnalvoidonCreate(nalBundlestate){
super.onCreate(state);
LinearLayout.LayoutParamscontainerParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,0.0F);
LinearLayout.LayoutParamswidgetParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,
1.0F);
LinearLayoutroot=newLinearLayout(this);
root.setOrientaaon(LinearLayout.VERTICAL);
root.setBackgroundColor(Color.LTGRAY);
root.setLayoutParams(containerParams);
LinearLayoutll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.GRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
EditTexttb=newEditText(this);
tb.setText(R.string.defaultLeJText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
tb=newEditText(this);
tb.setText(R.string.defaultRightText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
ll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.DKGRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
Bufonb=newBuCon(this);
b.setText(R.string.labelRed);
b.setTextColor(Color.RED);
b.setLayoutParams(widgetParams);
ll.addView(b);
b=newBuCon(this);
b.setText(R.string.labelGreen);
b.setTextColor(Color.GREEN);
b.setLayoutParams(widgetParams);
ll.addView(b);
setContentView(root);
}
}

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);

publicnalvoidonCreate(nalBundlestate){
super.onCreate(state);
LinearLayout.LayoutParamscontainerParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,0.0F);
LinearLayout.LayoutParamswidgetParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,
1.0F);
LinearLayoutroot=newLinearLayout(this);
root.setOrientaaon(LinearLayout.VERTICAL);
root.setBackgroundColor(Color.LTGRAY);
root.setLayoutParams(containerParams);
LinearLayoutll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.GRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
EditTexttb=newEditText(this);
tb.setText(R.string.defaultLeJText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
tb=newEditText(this);
tb.setText(R.string.defaultRightText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
ll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.DKGRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
Bufonb=newBuCon(this);
b.setText(R.string.labelRed);
b.setTextColor(Color.RED);
b.setLayoutParams(widgetParams);
ll.addView(b);
b=newBuCon(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);
E se fosse!
Button b = new Button(this);
b.setText(R.string.labelRed);
vertical?!
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);

publicnalvoidonCreate(nalBundlestate){
super.onCreate(state);
LinearLayout.LayoutParamscontainerParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,0.0F);
LinearLayout.LayoutParamswidgetParams=newLinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,
1.0F);
LinearLayoutroot=newLinearLayout(this);
root.setOrientaaon(LinearLayout.VERTICAL);
root.setBackgroundColor(Color.LTGRAY);
root.setLayoutParams(containerParams);
LinearLayoutll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.GRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
EditTexttb=newEditText(this);
tb.setText(R.string.defaultLeJText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
tb=newEditText(this);
tb.setText(R.string.defaultRightText);
tb.setFocusable(false);
tb.setLayoutParams(widgetParams);
ll.addView(tb);
ll=newLinearLayout(this);
ll.setOrientaaon(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.DKGRAY);
ll.setLayoutParams(containerParams);
root.addView(ll);
Bufonb=newBuCon(this);
b.setText(R.string.labelRed);
b.setTextColor(Color.RED);
b.setLayoutParams(widgetParams);
ll.addView(b);
b=newBuCon(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);

Discusso
Algumaquijprogramouinterfacesgrcas
dessaforma?
Quaisasdesvantagensdesseapode
abordagem?
Equaisseriamasalternaavas?

rvoresdeComponentes

Composite:
Vises so
undo
g
e
s
s
a
d
a
p
u
agr
de
um padro
ado
m
a
h
c
s
o
t
e
j
pro
gum
l
A
.
e
t
i
s
o
p
com
disso?
r
a
l
a
f
u
i
v
u
j o

LinearLayout
(veracal)

LinearLayout
(Horizontal)

Text

LinearLayout
(Horizontal)

Text

Bufon

Bufon

Padres de projeto:
O que isso mesmo?

PadresdeProjeto
Soluescomunsparaproblemasrecorrentes
emprogramao.
Padrescriacionais.
Padresestruturais.
Padrescomportamentais.

Composite
Aidiadessepadrodeprojetostratar
objetoscompostoseobjetosprimiavosda
mesmaforma.
No caso de
Android, qu
ais
classes faze
mo
papel de Lea
fe
Composite?

SeparandoVisoeModelo
Androidpermiteque
interfacesgrcassejam
projetadasemXML
XMLpermitequervores
decomponentesgrcos
sejamconstrudascom
facilidade.
OAndroidSDK
disponibilizaumeditor
deinterfacesgrcas.

<LinearLayoutxmlns:android="hRp://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientaaon="verZcal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientaaon="horizontal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/text1"
android:text="@string/defaultLeJText"
android:focusable="false"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
<EditText
android:id="@+id/text2"
android:text="@string/defaultRightText"
android:focusable="false"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientaaon="horizontal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<BuCon
android:id="@+id/buRon1"
android:text="@string/labelRed"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
<BuCon
android:id="@+id/buRon2"
android:text="@string/labelGreen"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

rvoresXML

<LinearLayout>
<LinearLayout>
<EditText/>
Algum v
iu
<EditText/>
uma rvo
re
</LinearLayout>
aqui?
<LinearLayout>
<Button/>
<Button/>
</LinearLayout>
</LinearLayout>

LigandoosFios
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);
}
}

Aseparaoentrevisoemodelo
permitequeocdigodemodelo
quemenoremaisclaro.

<LinearLayoutxmlns:android="hRp://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientaaon="verZcal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientaJon="horizontal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/text1"
android:text="@string/defaultLe>Text"
android:focusable="false"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
<EditText
android:id="@+id/text2"
android:text="@string/defaultRightText"
android:focusable="false"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientaaon="horizontal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<Bufon
android:id="@+id/buRon1"
android:text="@string/labelRed"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
<Bufon
android:id="@+id/buRon2"
android:text="@string/labelGreen"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

<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
E se fosse 1.5?!
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>

<LinearLayoutxmlns:android="hRp://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientaaon="verZcal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientaJon="horizontal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/text1"
android:text="@string/defaultLe>Text"
android:focusable="false"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
<EditText
android:id="@+id/text2"
android:text="@string/defaultRightText"
android:focusable="false"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientaaon="horizontal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<Bufon
android:id="@+id/buRon1"
android:text="@string/labelRed"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
<Bufon
android:id="@+id/buRon2"
android:text="@string/labelGreen"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

<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.5"/>
<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>

<LinearLayoutxmlns:android="hRp://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientaaon="verZcal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientaaon="horizontal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/text1"
android:text="@string/defaultLeJText"
android:focusable="false"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
<EditText
android:id="@+id/text2"
android:text="@string/defaultRightText"
android:focusable="false"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientaaon="horizontal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<Bufon
android:id="@+id/buRon1"
android:text="@string/labelRed"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
<Bufon
android:id="@+id/buRon2"
android:text="@string/labelGreen"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText rve
e se
u
q
android:id="@+id/text1"
a
Par
android:text="@string/defaultLeftText"
se
s
e android:focusable="false"
r?
o
d
a
c
entifi
idandroid:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.5"/>
<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>

<LinearLayoutxmlns:android="hRp://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientaaon="verZcal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientaaon="horizontal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/text1"
android:text="@string/defaultLeJText"
android:focusable="false"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
<EditText
android:id="@+id/text2"
android:text="@string/defaultRightText"
android:focusable="false"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientaaon="horizontal"
android:layout_width="ll_parent"
android:layout_height="wrap_content">
<Bufon
android:id="@+id/buRon1"
android:text="@string/labelRed"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
<Bufon
android:id="@+id/buRon2"
android:text="@string/labelGreen"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText nar
icio
d
a
android:id="@+id/text1"
o
Com o nosso
android:text="@string/defaultLeftText"
es a
r
o
c android:focusable="false"
?
t
u
o
y
laandroid:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.5"/>
<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>

CorescomoRecursos
PodemosdenircoresemumarquivoXMLem
res/values/colors.xml.
r esses
a
s
u
o
m
o
c
E
nosso
m
e
s
o
s
r
u
rec
layout
arquivo de
XML?

Como cor
es
so
codificada
s?

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


<resources>
<color name="red">#ffff0000</color>
<color name="green">#ff00ff00</color>
</resources>

AtributosdeCores
<Button
android:id="@+id/button1"
android:text="@string/labelRed"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textColor="@color/red"/>

XML
XMLumformatoparaconstruode
rvores.
Formatoquepodeserlidoporhumanos.

Mas,dadoqueXMLumformatoto
verboso,noseriainecienteuslono
mundorestritodeumdisposiavomvel?

VisoProceduralvsVisoDeclaraava
Interfacesgrcaspodemsercriadasem
cdigoJava.
Maneiraproceduraldeescreverprogramas.

Mastambmpodemsercriadasemarquivos
XML.
Maneiradeclaraavadeescreveroprograma.
formas
s
a
r
t
u
o
e
u
Q
ao
de program
oc
v
a
v
i
t
a
r
a
l
c
de
conhece?

ObtendoAjuda
AndroidpossuiumaAPImuitovasta.
MashmuitaajudadisponvelnaInternet.

Adicionandoeventos
Interfacesgrcasinteragemcomusuriosvia
eventos.
private int numClicks = 0;
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
final EditText tb1 = (EditText) findViewById(R.id.text1);
final EditText tb2 = (EditText) findViewById(R.id.text2);
((Button) findViewById(R.id.button2))
.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
tb1.setText(String.valueOf(numClicks++));
tb2.setText(String.valueOf(numClicks++));
}
});
}

FunesdeAltaOrdem
Simulandofunesdealtaordem.
Qual a
classe desse
objeto?

Como
poderamos
m
fazer isso e
Python?

private int numClicks = 0;


public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
final EditText tb1 = (EditText) findViewById(R.id.text1);
final EditText tb2 = (EditText) findViewById(R.id.text2);
((Button) findViewById(R.id.button2))
.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
tb1.setText(String.valueOf(numClicks++));
tb2.setText(String.valueOf(numClicks++));
}
});
}

Idenacadores
Interfacesgrcasinteragemcomusuriosvia
eventos.
Onde foram
declarados
esses
?
s
e
r
o
d
a
c
fi
i
t
iden

private int numClicks = 0;


public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
final EditText tb1 = (EditText) findViewById(R.id.text1);
final EditText tb2 = (EditText) findViewById(R.id.text2);
((Button) findViewById(R.id.button2))
.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
tb1.setText(String.valueOf(numClicks++));
tb2.setText(String.valueOf(numClicks++));
}
});
}

Observadores
Obotoagoraumobservadordeeventos.
Queobservaeventosdeclique.

Mas,muitosaparelhosnopossuemmouse.
Oqueumcliqueento?

Comparalhandoouvidos
Umescutadorpodeobservareventosem
vrioselementos.
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
final EditText tb1 = (EditText) findViewById(R.id.text1);
final EditText tb2 = (EditText) findViewById(R.id.text2);
Button.OnClickListener listener = new Button.OnClickListener() {
public void onClick(View arg0) {
tb1.setText(String.valueOf(numClicks++));
tb2.setText(String.valueOf(numClicks++));
}
};
((Button) findViewById(R.id.button1)).setOnClickListener(listener);
((Button) findViewById(R.id.button2)).setOnClickListener(listener);
}

Discusso
Almdeeventosdeclique,emqueoutros
aposdeeventospodemospensar?
Quaissoasenadadesqueescutam
eventos?
Equaissoasenadadesquetratam
oseventos?
Oqueissotemavercomopadro
modelovisocontrolador?

PadrodeProjetosObserver
Umobjeto,osujeito,temacapacidadede
percebereventosenoacaroutrosobjetos,os
observadores.
Observadoressabemsernoacadosde
eventosquesodetectadospelosujeito.
O que um
padro de
projetos?

Quem o
sujeito em
nosso ltimo
exemplo?

Quem o
observado
r
naquele
exemplo?

Como ficaria
esse padro
de projetos
em UML?

ObserveremUML

hfp://en.wikipedia.org/wiki/Observer_pafern

Exerccio:anagramas
EscrevaumaaavidadeAnagramAcavityque
determineseduasstringssoanagramas.
UseumToastparareportarseasstringsso
anagramasouno.

VisodeAnagramas

<LinearLayout
xmlns:android="hfp://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="ll_parent"
android:layout_height="wrap_content"
android:orientaaon="veracal">
<EditText
android:id="@+id/text1"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"
android:focusable="true"
android:text="@string/defaultLevText"/>
<EditText
android:id="@+id/text2"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"
android:focusable="true"
android:text="@string/defaultRightText"/>
<Bufon
android:id="@+id/bufon1"
android:layout_width="ll_parent"
android:layout_height="ll_parent"
android:layout_weight="1"
android:text="@string/labelAnagram"/>
</LinearLayout>

anagrams.xml

ResolvendoAnagramas
privatebooleananagrams(nalStrings1,nalStrings2){
booleanyes=true;
if(s1.length()!=s2.length()){
yes=false;
}else{
int[]table=newint[TABLE_SIZE];
for(inti=0;i<s1.length();i++){
table[s1.charAt(i)]++;
}
for(inti=0;i<s2.length();i++){
if(table[s2.charAt(i)]==0){
yes=false;
break;
}else{
table[s2.charAt(i)];
}
}
}
returnyes;
}

Qualacomplexidadedo
programaaolado?
Eemtermosdeespao,
qualacomplexidade
dessasoluo?

LigandoosFios
publicnalvoidonCreate(nalBundlestate){
super.onCreate(state);
setContentView(R.layout.anagrams);
nalEditTexttb1=(EditText)ndViewById(R.id.text1);
nalEditTexttb2=(EditText)ndViewById(R.id.text2);
((Bufon)ndViewById(R.id.bufon1))
.setOnClickListener(newBufon.OnClickListener(){
publicvoidonClick(nalViewarg0){
Contextcontext=getApplicaaonContext();
intduraaon=Toast.LENGTH_SHORT;
CharSequencetext="Naosaoanagramas!";
if(anagrams(tb1.getText().toString(),tb2.getText().toString())){
text="Anagrams!";
}
Toasttoast=Toast.makeText(context,text,duraaon);
toast.show();
}
});
}

Você também pode gostar