Upload
khalil-mighri
View
220
Download
0
Embed Size (px)
Citation preview
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 1/22
Atelier 2Page 1
ISITCom H-S
ISIT-Android Khalil MIGHRI
ISIT-Android
ISITCom Android Club
Atelier 2: Appplication « Prix-Solde »
« Création d’une interface graphique & Manipulation des
activités et Sous-activités »
Assisté par : Khalil MIGHRI
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 2/22
Atelier 2Page 2
ISITCom H-S
ISIT-Android Khalil MIGHRI
I- Création d’un Emulateur virtuel :
Window AVD Manager:
New
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 3/22
Atelier 2Page 3
ISITCom H-S
ISIT-Android Khalil MIGHRI
Donner un nom du téléphone Android Virtuel, choisir la version d’Android à utiliser.
Ce sont les paramètres les plus importants.
Create AVD
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 4/22
Atelier 2Page 4
ISITCom H-S
ISIT-Android Khalil MIGHRI
Le nouvel appareil virtuel est bien ajouter dans la liste, pour tester : sélectionner AVD1Start
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 5/22
Atelier 2Page 5
ISITCom H-S
ISIT-Android Khalil MIGHRI
Launch
En fonction de vos besoins, vous pouvez créer autant de téléphone virtuel que nécessaires.
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 6/22
Atelier 2Page 6
ISITCom H-S
ISIT-Android Khalil MIGHRI
II- Application « Prix-Solde » :
Ouvrir Eclipse
file New Other :
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 7/22
Atelier 2Page 7
ISITCom H-S
ISIT-Android Khalil MIGHRI
Choisir : Android Android Project Next
Next
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 8/22
Atelier 2Page 8
ISITCom H-S
ISIT-Android Khalil MIGHRI
Cocher la version d’Android que vous voulez l’utiliser Next
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 9/22
Atelier 2Page 9
ISITCom H-S
ISIT-Android Khalil MIGHRI
Donner le nom du package qui doit être sous la forme « your.package.namespace »
Par convention, le nom d'un package est sous la forme d'une "url à l'envers". Vous
pouvez mettre votre nom de domaine personnel ou ce que vous voulez.
RQ : Vérifier que la case « Create Activity » est coché.
Finish
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 10/22
Atelier 2Page 10
ISITCom H-S
ISIT-Android Khalil MIGHRI
Notre projet est bien crée, et on obtient la structure du projet suivante :
Le fichier « src/atelier2.prixsolde.isitandroid/PrixsoldeActivity.java » par défaut contient :
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 11/22
Atelier 2Page 11
ISITCom H-S
ISIT-Android Khalil MIGHRI
Ajoutons le reste des méthodes de base d’une activité : onStart(), onRestart(), onResume(),
onPause(), onStop() ,onDestroy()
Donc, le fichier « PrixsoldeActivity.java » devient :
package atelier2.prixsolde.isitandroid;
import android.app.Activity;import android.os.Bundle;
public class PrixsoldeActivity extends Activity {/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.main);
}@Override protected void onStart() {
// TODO Auto-generated method stub super.onStart();
}@Override protected void onRestart() {
// TODO Auto-generated method stub super.onRestart();
}@Override protected void onResume() {
// TODO Auto-generated method stub super.onResume();
}
@Override protected void onPause() {
// TODO Auto-generated method stub super.onPause();
}@Override protected void onStop() {
// TODO Auto-generated method stub super.onStop();
}@Override protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();}
}
Maintenant, on va modifier le fichier « res/Layout/main.xml »qui va contenir l’interface graphique
de notre activité :
Le contenu par défaut :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 12/22
Atelier 2Page 12
ISITCom H-S
ISIT-Android Khalil MIGHRI
android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"
/></LinearLayout>
Ajoutons 2 éléments « EditText » et un bouton le fichier « main.xml » devient :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
<EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/editText1" android:hint="@string/edittext1">
</EditText> <EditText
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/editText2" android:hint="@string/solde"></EditText>
<Button android:layout_height="wrap_content" android:id="@+id/button1" android:text="Go" android:layout_width="172dp">
</Button> </LinearLayout>
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 13/22
Atelier 2Page 13
ISITCom H-S
ISIT-Android Khalil MIGHRI
L’interface graphique devient :
Maintenant, on va modifier le fichier « accueil.java » pour qu’on puisse accéder aux éléments
ajoutés :
package atelier2.prixsolde.isitandroid;
import android.app.Activity;import android.os.Bundle;
public class PrixsoldeActivity extends Activity {
//déclaration des variables EditText edit1 ;EditText edit2;Button bouton;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.main);
//appel des composants ajouter dans le fichier "main.xml" edit1=(EditText)this.findViewById(R.id.editText1);edit2=(EditText)this.findViewById(R.id.editText2);bouton =(Button)this.findViewById(R.id.button1);
}@Override protected void onStart() {
// TODO Auto-generated method stub super.onStart();
}@Override
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 14/22
Atelier 2Page 14
ISITCom H-S
ISIT-Android Khalil MIGHRI
protected void onRestart() {// TODO Auto-generated method stub super.onRestart();
}@Override protected void onResume() {
// TODO Auto-generated method stub super.onResume();}@Override protected void onPause() {
// TODO Auto-generated method stub super.onPause();
}@Override protected void onStop() {
// TODO Auto-generated method stub super.onStop();
}
@Override protected void onDestroy() {// TODO Auto-generated method stub super.onDestroy();
}}
Ensuite, on va ajouter l’action au bouton, pour qu’en cliquant, le prix après solde s’affiché dans une
nouvelle interface.
Donc le code du fichier « PrixsoldeActivity » devient :
package atelier2.prixsolde.isitandroid;
import android.app.Activity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class PrixsoldeActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//déclaration des variables EditText edit1;
EditText edit2;
Button bouton;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. main);
//appel des composants ajouter dans le fichier "main.xml" edit1 = (EditText) this.findViewById(R.id.editText1);
edit2 = (EditText) this.findViewById(R.id.editText2);bouton = (Button) this.findViewById(R.id.button);
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 15/22
Atelier 2Page 15
ISITCom H-S
ISIT-Android Khalil MIGHRI
bouton.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
double r = Double. parseDouble(edit1.getText().toString()) *
Double. parseDouble(edit2.getText().toString());
r=r/100;r=Double. parseDouble(edit1.getText().toString())-r;Intent intent = new Intent(getApplicationContext(),
affichage.class);intent.putExtra("data", r);
startActivity(intent);}
});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub super.onStop();
}@Override
protected void onDestroy() {
// TODO Auto-generated method stub super.onDestroy();
}
}
Il nous reste que de créer un nouveau le sous activité « affichage » :
Sélectionner le nom du package de notre projet clique droite New Class
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 16/22
Atelier 2Page 16
ISITCom H-S
ISIT-Android Khalil MIGHRI
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 17/22
Atelier 2Page 17
ISITCom H-S
ISIT-Android Khalil MIGHRI
Donner le nom du classe « affichage » Finish
Mettre le code suivant dans le fichier « affichage.java » :
package atelier2.prixsolde.isitandroid;
import prixsold.isitandroid.club.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class affichage extends Activity{TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.main2);
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 18/22
Atelier 2Page 18
ISITCom H-S
ISIT-Android Khalil MIGHRI
textview= (TextView) this.findViewById(R.id.textview);
Double s = this.getIntent().getExtras().getDouble("data");textview.setText("Le prix après solde est: "+s);
}
}
Enfin, créer le fichier « main2.xml » sous le dossier ‘Layout’ :
Séléctionner « Layout » clique droite New Android XML File
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 19/22
Atelier 2Page 19
ISITCom H-S
ISIT-Android Khalil MIGHRI
Finish
Ajouter un «élément « TextView » :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview" />
</LinearLayout>
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 20/22
Atelier 2Page 20
ISITCom H-S
ISIT-Android Khalil MIGHRI
Il faut ajouter l’activité « affichage » dans le fichier « AndroidManifest.xml » sous le nœud application, ce fichier est placé dans le répertoire de base du projet.
<activity android:name=".affichage" android:label="Prix après Solde">
</activity>
III-Test :
Exécution du program :
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 21/22
Atelier 2Page 21
ISITCom H-S
ISIT-Android Khalil MIGHRI
8/3/2019 Atelier2.PrixSolde.isit Android
http://slidepdf.com/reader/full/atelier2prixsoldeisit-android 22/22