Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Les events : évenements

Ngor SECK
ngorsecka@gmail.com
1. ButtonEvent
public class ConnexionActivity extends AppCompatActivity {
private EditText txtLogin, txtPassword;
private Button btnExit, btnConnexion;
private String login, password;

txtLogin=(EditText)findViewById(R.id.txtLogin);
txtPassword=(EditText)findViewById(R.id.txtPassword);

btnConnexion=(Button)findViewById(R.id.btnConnect);
btnExit=(Button)findViewById(R.id.btnExit);

btnConnexion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

login=txtLogin.getText().toString();
password=txtPassword.getText().toString();

if(login.equals("") || password.equals(""))
{

Toast.makeText(ConnexionActivity.this, "Veuillez renseigner les champs",


Toast.LENGTH_LONG).show() ;
}else{
Intent intent = new Intent(ConnexionActivity.this, PresentationActivity.class);
startActivity(intent);
}
}
}
Une autre méthode
ex5.3 : main.xml
<?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">
<Button
android:id="@+id/bouton"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Appuyer"
/>
</LinearLayout>
//*************code java
ex5.3 : Principale.java
package org.education.lgf.atelier;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
public class Principale extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button monbouton =(Button)findViewById(R.id.bouton);
monbouton.setOnClickListener(this);
}
public void onClick(View view){
Toast.makeText(this,"BRAVO", Toast.LENGTH_SHORT).show();
}
}

2. CheckBoxEvent
ex5.7 : main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box"
android:text="Cocher ou décocher la case"
/>
</LinearLayout>

ex5.7 : Principale.java
package org.education.lgf.atelier;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class Principale extends Activity {
/** Called when the activity is first created. */
CheckBox maboite;
@Override//ceci signifie une redefinition et non une sercharge
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
maboite=(CheckBox)findViewById(R.id.box);
maboite.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener (){
public void onCheckedChanged (CompoundButton buttonView, boolean isChecked){
if (isChecked==true){
Toast.makeText(Principale.this,"La case est cochée",
Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(Principale.this,"La case est décochée",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
//****************************************************************************************
ex5.8 : main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box_1"
android:text="Box 1"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box_2"
android:text="Box 2"
/>
</LinearLayout>

ex5.8 : Principale.java
package org.education.lgf.atelier;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class Principale extends Activity implements CompoundButton.OnCheckedChangeListener {
/** Called when the activity is first created. */
CheckBox box1;
CheckBox box2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
box1=(CheckBox)findViewById(R.id.box_1);
box1.setOnCheckedChangeListener(this);
box2=(CheckBox)findViewById(R.id.box_2);
box2.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if (buttonView==box1){
if (isChecked){
Toast.makeText(Principale.this,"Box 1 cochée", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(Principale.this,"Box 1 décochée", Toast.LENGTH_LONG).show();
}
}
else {
if (isChecked){
Toast.makeText(this,"Box 2 cochée", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this,"Box 2 décochée", Toast.LENGTH_LONG).show();
}
}
}
}

3. DatePicker
ex5.10 : main.xml
<?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">
<DatePicker
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/date"
/>
</LinearLayout>

ex5.10 : Principale.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.Toast;
public class Principale extends Activity {
DatePicker choixDate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
choixDate=(DatePicker)findViewById(R.id.date);
choixDate.init(2010,10,22,new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth){
Toast.makeText(Principale.this,"Voici la nouvelle Date :\nAnnée :"+
year+" Mois : "+monthOfYear+" Jour : "+dayOfMonth, Toast.LENGTH_LONG).show();
}
});
}
}

4. RadioButtonEvenent
ex5.9 : main.xml
<?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">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/monRadioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RaOUI"
android:text="OUI">
</RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RaNON"
android:text="NON">
</RadioButton>
</RadioGroup>
</LinearLayout>

ex5.9 : Principale.java
package org.education.lgf.atelier;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Principale extends Activity {
RadioGroup radiog;
RadioButton radiob_oui;
RadioButton radiob_non;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

radiog=(RadioGroup)findViewById(R.id.monRadioGroup);
radiob_oui=(RadioButton)findViewById(R.id.RaOUI);
radiob_non=(RadioButton)findViewById(R.id.RaNON);

radiog.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if ((RadioButton)findViewById(checkedId)==radiob_oui){
Toast.makeText(Principale.this,"OUI", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Principale.this,"NON", Toast.LENGTH_SHORT).show();
}
}
});
}
}

You might also like