Académique Documents
Professionnel Documents
Culture Documents
Zaki
Enseignant B. Zaki 88 associer la premire vue graphique l'activit principale de l'application : acceuil set)ontent iew*+.layo&t.acce&il,9 protected void on,estroy() 6 9 protected void on1ause() 69 protected void on:esume() 6 9 protected void on%tart() 6 9 protected void on%top() 6 9 9
Interfaces graphiques :
"es lments graphi*ues hritent de la classe (iew$ 0n peut regrouper des lments graphi*ues dans une (iew)roup$ ,es (iew)roup particuliers sont prdfinis: ce sont des ga!arits (layo&t)$ 4n ga!arit peut contenir des lments graphi*ues& ou d'autres ga!arits *ui proposent une prdispositions des o!'ets graphi*ues: LinearLayout: dispose les lments de gauche droite ou du haut vers le !as elativeLayout: les lments enfants sont placs les uns par rapport aux autres *a$leLayout: disposition matricielle - +rameLayout: disposition en haut gauche en empilant les lments 4n composant graphi*ue peut tre : 1. La$el de te&te : -ext;iew En XML: <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:id="@+id/le_texte" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" android:layout_gravity="center" /> </LinearLayout> Par la programmation: public class Activity2 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout gabarit = new LinearLayout(this); gabarit.setGravity(Gravity.CENTER); // centrer les lments graphiques gabarit.setOrientation(LinearLayout.VERTICAL); // empiler vers le bas ! TextView texte = new TextView(this); texte.setText("Programming creation of interface !"); gabarit.addView(texte); setContentView(gabarit);} 2. ,ones de te&te : %dit*e&t 3. Bouton : Button Button b = (Button)findViewById(R.id.Button01);// Boutton01 est !I" du #outon dans e fic$ier layout.xml 4. Image : Image(iew : 5. C-oi& multiple .C-ec/$o&0 : la mthode setChecked() permet de modifier l+tat de la case et la mthode isC-ec/ed.0 permet de vrifier si l+lment est slectionn$ ,ans le fichier xml : <CheckBox android:id="@+id/chk" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chk_android" android:checked="true" />
Enseignant B. Zaki CheckBox checkBox = (CheckBox) findViewById(R.id.chk); if (checkBox.isChecked()) {checkBox.setChecked(false); } Afin d+a'outer un vnement vote /hec<Box& il faut : =$ a'outer dans le fichier 23" cette ligne : android:onClick="onCheckboxClicked" : <CheckBox android:id="@+id/chk" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chk_android" android:checked="true" android:onClick="onCheckboxClicked" /> >$ ,ans l+activit *ui utilise ce votre /hec<Box& il faut implmenter une mthode *ui poss(de le m me nom *ue la valeur de on)lick : onC-e/$o&Cli/ed public void onCheckboxClicked(View view) { // if un view est slectionn boolean checked = ((CheckBox) view).isChecked(); // vrifiant quel checkbox nous avons cliqu switch(view.getId()) { case R.id.checkbox_1: if (checked) // quelques chose ici break; case R.id.checkbox_2: if (checked) // quelques choses ici break; } 6. adioButton : les !outons radio doivent tre groups en utilisant adio)roup comme l+indi*ue l exemple suivant :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RadioGroup android:id="@+id/radioGender" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/genderMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Male" android:checked="true" /> <RadioButton android:id="@+id/genderFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" /> </RadioGroup> <Button android:id="@+id/btnDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Here" /> </LinearLayout>
Enseignant B. Zaki %t dans votre code : public class RadioButtonExample extends Activity implements OnClickListener{ private RadioGroup radioSexGroup; private RadioButton radioSexButton; private Button btnDisplay; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_radio_button_example); radioSexGroup = (RadioGroup) findViewById(R.id.radioGender); btnDisplay = (Button) findViewById(R.id.btnDisplay); btnDisplay.setOnClickListener(thi) ; public void onClick(View v) { // rcuprer lID du bouton radio slectionn int selectedId = radioSexGroup.getCheckedRadioButtonId(); // chercher le bouton radio dans le fichier XML radioSexButton = (RadioButton) findViewById(selectedId); Toast.makeText(RadioButtonExample.this,radioSexButton.getText(), Toast.LENGTH_SHORT).show(); } } 7. Liste : Liste(iew : Etant donn *u'une liste peut contenir des lments graphi*ues divers et varis& les lments de la liste doivent tre insrs dans un ListAdapter et il faut aussi dfinir le ga!arit *ui sera utilis pour afficher cha*ue lment du ListAdapter$ 1renons un exemple simple: une liste de chaine de caract(res$ ,ans ce cas& on cr un nouveau ga!arit monte&te et on a'oute dynami*uement un ArrayAdapter la liste list(iew1. %&emple : ListView list = (ListView)findViewById(R.id.listView1); ArrayAdapter<String> tableau = new ArrayAdapter<String>(list.getContext(), R.layout.montexte); for (int i=0; i<40; i++) { tableau.add("coucou " + i); } list.setAdapter(tableau);
Enseignant B. Zaki
Lancer une nouvelle activit2 dans la m3me application : crer l'#ntent et passer la classe de l'activit ci!le par l'#ntent: Intent login = new Intent(this, GiveLogin.class); startActivity(login); Dans le constructeur, tthis reprsente le contexte de l application! Dans certain cas, il ne faut pas mettre this mais faire appel " getApplicationContext() si l ob#et manipulant l Intent n hriste pas de Context! Lancer une nouvelle activit2 dans autre application : on donne au constructeur de l $ntent les donnes et l %&$ cible' l O( est char) de trouver une application pouvant rpondre " l $ntent! *utton b + ,*utton-find.ie/*0$d,&!id!*utton12-3 b!setOnClickListener,this- 3 public void onClick,.ie/ v- 4 %ri telnumber + %ri!parse,5tel'16787871115-3// %&L cible $ntent call + new $ntent,$ntent!9C:$O;<D$9L, telnumber-3 start9ctivit0,call-3 =