9

GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents

Embed Size (px)

DESCRIPTION

Slides du Bootcamp d'Initiation à Android organisé le 19/09/2013 par le GDG Rennes LiveCoding exercise #2 : Hello World with Events and Intents

Citation preview

Page 1: GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents
Page 2: GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents

LiveCoding : Hello World avec Intents et Events

● Objectif : Première application interactive○ Un premier écran avec un champ pour saisir son nom et un bouton

qui amène au deuxième écran○ Un deuxième écran avec un Hello $NOM personnalisé

● Concepts à voir○ Création d'une application avec plusieurs Activités○ Création d'un Gabarit à plusieurs composants○ Utilisation des Event Listeners pour écouter des Évenements○ Utilisation des Intent pour changer d'Activité

Page 3: GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents

LiveCoding : Hello World avec Intents et Events

● Code du LiveCoding : déppot GitHubhttps://github.com/LostInBrittany/gdgrennes-androidbootcamp-HelloWorld

● Chaque step correspond à une branchegit checkout step-XX

Page 4: GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents

Step-1 : Layout de l’Activity principale

● En utilisant le Designer d’Android Studio

Mais l’i18n, c’est où ?

Page 5: GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents

Step-2 : i18n des chaînes

Page 6: GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents

Step-3 : Création de HelloActivity

Page 7: GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents

Step-4 : EventListener basique sur le bouton

Page 8: GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents

Step-5 : Intent Filter & Intent

Dans le l’Activité principale@Override

protected void onStart() {

super.onStart();

Button b = (Button)findViewById(R.id.buttonOK);

b.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

EditText nameField =

(EditText) findViewById(R.id.editTextName)

String name = nameField.getText().toString();

Intent helloIntent =

new Intent("org.gdgrennes.bootcamp.android.HELLO");

helloIntent.putExtra("name", name);

startActivity(helloIntent);

}

});

}Dans le Manifeste<activity

android:name="org.gdgrennes.android.bootcamp.HelloActivity"

android:label="@string/title_activity_hello" >

<intent-filter>

<action android:name="org.gdgrennes.bootcamp.android.HELLO" />

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</activity>

Dans HelloActivity@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_hello);

Bundle extras = getIntent().getExtras();

String name = extras.getString("name");

TextView nameLabel =

(TextView) findViewById(R.id.textHello);

nameLabel.setText(nameLabel.getText()+" "+name);

}

Page 9: GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents

LiveCoding : Hello World avec Intents et Events