Sie sind auf Seite 1von 13

1-

a. A quoi sert l’attribut android :layout_gravity= ‘’center’’ ?

en utilise l’attribut layout_gravity= ‘’center’’ pour center le Botton ou le textView


horizontalement

b. Dessiner l’interface que vous allez obtenez avec ce layout, en respectant


l’emplacement des éléments et leurs étiquettes. ?
c. A quoi sert l’élément Radio Group ?
son but est de faire en sorte qu’il puisse n’y avoir qu’un seul radio Button
sélectionné dans tout le groupe
d. Citez l’instruction qui permet d’identifier le dernier textview dans la classe.java.
?
TextView res =null ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res=(TextView)findViewById(R.id.res);
}

2- Si on veut qu’on exécute des instructions après que l’activité est fermée par
l’utilisateur comment peut-on procédé ?

@Override
public void onDestroy() {

super.onDestroy();

//Instruction
}
a- Donnez le code du layout.xml

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/customername"
android:hint="Customer name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/phone"
android:inputType="phone"
android:hint="Telephone"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email"
android:inputType="textEmailAddress"
android:hint="E-mail address"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Small"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bacon"
android:id="@+id/bacon"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Extra Cheese"
android:id="@+id/extraCheese"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Onion"
android:id="@+id/onion"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mushroom"
android:id="@+id/mashroom"/>
<EditText
android:id="@+id/ref"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Preferred Delivery Time"/>
<EditText
android:id="@+id/del"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Delivery instructions"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/submit"
android:layout_gravity="bottom"
android:text="SUBMIT ORDER"
/>

</LinearLayout>
b- Codez la classe .java qui permet de collecter les informations saisies par
l’utilisateur et l’enregistrer dans une base de données nommé order via le button
submit order
public class MainActivity extends AppCompatActivity {
EditText name ,phone ,email ,Del ,Ref;
CheckBox Bacon ,Extra_Cheese ,Mushroom ,Onion;
Button submit ;
SQLiteDatabase DB;
String Small ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.customername);
phone=(EditText)findViewById(R.id.phone);
email=(EditText)findViewById(R.id.email);
Del=(EditText)findViewById(R.id.del);
Ref=(EditText)findViewById(R.id.ref);
submit = (Button)findViewById(R.id.submit);
Bacon = (CheckBox)findViewById(R.id.bacon) ;
Extra_Cheese = (CheckBox)findViewById(R.id.extraCheese)
;
Onion = (CheckBox)findViewById(R.id.onion) ;
Mushroom = (CheckBox)findViewById(R.id.mashroom) ;
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

DB=openOrCreateDatabase("order",MODE_PRIVATE,null);
DB.execSQL("CREATE TABLE IF NOT EXISTS
CUSTOMER(num integer primary key autoincrement,name VARCHAR
,num_tele VARCHAR ,email VARCHAR ,small VARCHAR ,Pref VARCHAR
,del VARCHAR);");

if(Bacon.isChecked())
Small+="Bacon ";
if(Extra_Cheese.isChecked())
Small+="Extra_Cheese ";
if(Onion.isChecked())
Small+="Onion ";
if(Mushroom.isChecked())
Small+="Mushroom ";
if (name.getText().toString().trim().length()!=0
&&

phone.getText().toString().trim().length()!=0 &&

email.getText().toString().trim().length()!=0 &&

Del.getText().toString().trim().length()!=0 &&

Ref.getText().toString().trim().length()!=0 &&
Small.length()!=0) {

DB.execSQL("INSERT INTO CUSTOMER(name


,num_tele,email,small,Pref,del) VALUES('" + name.getText() +
"','" + phone.getText() + "','" + email.getText() +
"','"+Small+"','" + Ref.getText() + "','" + Del.getText() + "');
");
Small = "";

});

. c- Créer un seconde class.java qui n’affiche aucun layout mais qui permet
d’afficher un Toast contenant les informations du dernier ordre effectué ?
public class secondAct extends AppCompatActivity {

SQLiteDatabase DB ;
Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
try {
DB = openOrCreateDatabase("order", MODE_PRIVATE,
null);
c = DB.rawQuery("SELECT * FROM CUSTOMER", null);
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
buffer.append("name: " + c.getString(1) + "\n");
buffer.append("tele: " + c.getString(2) + "\n");
buffer.append("Prénom: " + c.getString(3) +
"\n");
buffer.append("small: " + c.getString(4) +
"\n");
buffer.append("pref: " + c.getString(5) + "\n");
buffer.append("del: " + c.getString(6) + "\n");
}

c.close();
AfficheMessage("Customers", buffer.toString());

}catch(Exception ex) {

Toast.makeText(this, ex.toString(),
Toast.LENGTH_LONG).show();
}

}
public void AfficheMessage(String titre,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(titre);
builder.setMessage(message);
builder.show();
}
}
d- Créer un intent qui permet de passer de la première activité vers la deuxième,
le lancement sera effectué directement après l’insertion des informations dans la
base de données.

Intent i=new Intent(MainActivity.this,SecondAct.class);


startActivity(i);

4- On suppose qu’on a un layout contenant un spinner dont l’id est ‘’sp’’ créer une
classe java permettant d’afficher les éléments suivants : java, c++, .net, visual
basic, prolog dans le spinner.

Spinner sp =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner) findViewById(R.id.sp);

ArrayList<String> proLanguages =new ArrayList<String>();


proLanguages.add("java");
proLanguages.add("C++");
proLanguages.add(".net");
proLanguages.add("visual basic");
proLanguages.add("prolog");

ArrayAdapter<String> adapter = new


ArrayAdapter<String>(this
,android.R.layout.simple_spinner_dropdown_item,proLanguages);
sp.setAdapter(adapter);

5- Donnez trois exemples de permissions qu’on peut utiliser dans le manifest


d’une application.
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-
permission>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" ></uses-
permission>
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>

6- On suppose qu’on la boite de dialogue suivante :


a- Donnez le code de layout permettant d’afficher les éléments contenu dans
la boite de dialogue ?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip">

<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrer quelque chose ci-dessous :"
android:paddingBottom="10dip" />

<EditText
android:id="@+id/Edchamp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt" />

</RelativeLayout>

b- Donnez le code de la classe .java qui permet d’afficher cette boite de dialogue,
le boutton ok permet d’afficher un toast contenant le texte tappé dans l’edit text
le bouton annuler permet de fermer la boite de dialogue.
public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LayoutInflater li = LayoutInflater.from(this);
final View alertDialog =
li.inflate(R.layout.activity_main, null);

AlertDialog.Builder builder = new


AlertDialog.Builder(this);

builder.setView(alertDialog);

builder.setTitle("Titre de notre boite de dialogue");

builder.setIcon(android.R.drawable.ic_dialog_alert);

builder.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
which) {

EditText Edchamp =
(EditText)alertDialog.findViewById(R.id.Edchamp);

Toast.makeText(MainActivity.this,
Edchamp.getText(), Toast.LENGTH_SHORT).show();
} });

builder.setNegativeButton("Annuler", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
which) {

} });
builder.show();
}

}
7- On suppose qu’on a une interface qui contient un edittext et trois radiobutton :
a- Donnez le code du layout.xml.

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text"
android:layout_marginTop="20dp"
android:hint="Enter un text "/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rouge"
android:text="Rouge"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vert"
android:text="Vert"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/jaune"
android:text="Jaune"/>

</RadioGroup>

</LinearLayout>
b- Donnez le code de la classe.java.

public class MainActivity extends AppCompatActivity implements


OnCheckedChangeListener {

RadioGroup radioColor ;
EditText txt ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioColor = (RadioGroup) findViewById(R.id.radioColor);
txt = (EditText) findViewById(R.id.txt);

radioColor.setOnCheckedChangeListener((OnCheckedChangeListener)t
his);

@Override
public void onCheckedChanged(RadioGroup group, int
checkedId) {
switch (checkedId)
{
case R.id.rouge:
txt.setTextColor(Color.parseColor("#ff0000"));
break;
case R.id.vert:
txt.setTextColor(Color.parseColor("#00ff00"));
break;
case R.id.jaune:
txt.setTextColor(Color.parseColor("#ffff00"));
break;
default:
break;
}
}
}
Ktee……..eeeb
3la ……
9albeeeek (:

Best wiches xD

Das könnte Ihnen auch gefallen