Bonjour,

Je fais une simple activité de calcul de tips (pourboires) dont voici le code et ça marche plutôt bien pour l'instant.

Tips.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.example.monappli;
 
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
 
public class Tips extends Activity implements SeekBar.OnSeekBarChangeListener {
 
  private final String defaut = "Vous devez cliquer"; // La chaîne de caractères par défaut  
  private final String defautpersonne = "1";
 
  Button envoyer = null;
  Button raz = null;	
  SeekBar mSeekBar;
  TextView ResMontantTip = null;  
  EditText EditMontantAddition;
  TextView mProgressText;
  TextView Niveau;
  EditText EditNombrePersonne;
  TextView ResTotal;
  TextView ResTotalPersonne;
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tips);
 
    // Récuperation des vues
    envoyer = (Button)findViewById(R.id.calcul);    	
    raz = (Button)findViewById(R.id.raz);
    mSeekBar = (SeekBar)findViewById(R.id.seek);     
    mProgressText = (TextView)findViewById(R.id.progress);
    Niveau = (TextView)findViewById(R.id.Niveau);
    ResMontantTip = (TextView)findViewById(R.id.ResMontantTip);    
    ResTotal = (TextView)findViewById(R.id.ResTotal);
    ResTotalPersonne = (TextView)findViewById(R.id.ResTotalPersonne);
    EditMontantAddition = (EditText)findViewById(R.id.EditMontantAddition);
    EditNombrePersonne = (EditText)findViewById(R.id.EditNombrePersonne);    
 
    // Attribution des listener adaptés aux vues
    EditNombrePersonne.setText(""  + String.valueOf(defautpersonne)); 
    mSeekBar.setOnSeekBarChangeListener(this);
    mSeekBar.setProgress(15);
    Niveau.addTextChangedListener(textWatcher);
    envoyer.setOnClickListener(envoyerListener);
    raz.setOnClickListener(razListener);
    EditMontantAddition.addTextChangedListener(textWatcher);
    EditNombrePersonne.addTextChangedListener(textWatcher);
    mProgressText.addTextChangedListener(textWatcher); 
  }
 
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
	  mProgressText.setText(progress + 10+ " " + getString(R.string.pourcentage));
	  mProgressText.setText(Integer.toString(progress));	
 
        if(progress<=9) {
        	Niveau.setText("" + getString(R.string.Mauvais));
        }       
        else if(progress>=10 && progress<=14)
        {        
        	Niveau.setText(getString(R.string.PasMal));
        }
        else if(progress>=15 && progress<=17)
        {
        	Niveau.setText(getString(R.string.Bien));
        }
        else if(progress>=18 && progress<=20)
        {
        	Niveau.setText(getString(R.string.Genial));    
        }
	}
	public void onStartTrackingTouch(SeekBar seekBar) {}	 
	public void onStopTrackingTouch(SeekBar seekBar) {}	  
 
  private TextWatcher textWatcher = new TextWatcher() {
 
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
      ResMontantTip.setText(defaut);
    }    
 
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
      int after) { }
 
    @Override
    public void afterTextChanged(Editable s) { }
  };
 
  // Bouton Calculer
  private OnClickListener envoyerListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
 
        String montant = EditMontantAddition.getText().toString(); //Récupèration du montant de l'addition        
        String personne = EditNombrePersonne.getText().toString(); // Récupèration du nombre de personnes 
        String tip = mProgressText.getText().toString(); // Récupèration du montant du tip
 
        float Vmontant = Float.valueOf(montant);
        float Vpersonne = Float.valueOf(personne);
        float Pourcentip = Float.valueOf(tip);              
 
       if(Vmontant == 0) // REGLER LE BUG DES CHAMPS VIDES!!
        //if (Vmontant.equals(""))
          Toast.makeText(Tips.this, "Pas cohérent", Toast.LENGTH_SHORT).show();
        else {        	
 
        	float Vtip = Vmontant * Pourcentip / 100;        	
        	float total = Vmontant + Vtip;
        	float totalp = total / Vpersonne;
 
        	ResMontantTip.setText("" + String.valueOf(Vtip)); 
        	ResTotal.setText("" + String.valueOf(total));
        	ResTotalPersonne.setText("" + String.valueOf(totalp));
        }
    }
  };
 
  // Listener RAZ
  private OnClickListener razListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
    	EditMontantAddition.getText().clear();
    	EditNombrePersonne.getText().clear();
    }
  };
  };
Le tips.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".MainActivity" >
 
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/SunsetTheme"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/MontantAddition"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="@string/TipsMontantAddition"
        android:textColor="@color/TextBlack"
        android:textStyle="bold" />
 
    <EditText
        android:id="@+id/EditMontantAddition"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/MontantAddition"
        android:ems="10"
        android:hint="@string/TipsMontantAddition"
        android:inputType="numberDecimal"
        android:textColor="@color/TextBrown"
        android:textColorHint="@color/Grey"
        android:textSize="@dimen/titres" />
 
    <TextView
        android:id="@+id/NombrePersonne"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/EditMontantAddition"
        android:gravity="center"
        android:text="@string/TipsNombre"
        android:textColor="@color/TextBlack"
        android:textStyle="bold" />
 
    <EditText
        android:id="@+id/EditNombrePersonne"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/NombrePersonne"
        android:ems="10"
        android:hint="@string/TipsNombre"
        android:inputType="numberDecimal"
        android:textColor="@color/TextBrown"
        android:textColorHint="@color/Grey"
        android:textSize="@dimen/titres" />
 
    <TextView
        android:id="@+id/MontantTip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/seek"
        android:gravity="center"
        android:text="@string/TipsMontantTip"
        android:textColor="@color/TextBlack"
        android:textSize="@dimen/textSize"
        android:textStyle="bold" />
 
    <TextView
        android:id="@+id/Pourcentage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/EditNombrePersonne"
        android:gravity="center"
        android:text="@string/TipsPourcentage"
        android:textColor="@color/TextBlack"
        android:textStyle="bold" />
 
    <SeekBar
        android:id="@+id/seek"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/Pourcentage"
        android:layout_toLeftOf="@+id/calcul"
        android:max="20"
        android:progress="1" />
 
    <TextView
        android:id="@+id/progress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/MontantTip"
        android:text="@string/pourcentage"
        android:textSize="@dimen/titres" />
 
    <TextView
        android:id="@+id/ResMontantTip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/progress"
        android:text="@string/Dollar"
        android:textSize="@dimen/titres" />
 
    <TextView
        android:id="@+id/TotalPersonne"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/ResMontantTip"
        android:gravity="center"
        android:text="@string/TipsTotalPersonne"
        android:textColor="@color/TextBlack"
        android:textStyle="bold" />
 
    <TextView
        android:id="@+id/ResTotalPersonne"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/TotalPersonne"
        android:text="@string/Dollar"
        android:textSize="@dimen/titres" />
 
    <TextView
        android:id="@+id/Total"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/ResTotalPersonne"
        android:gravity="center"
        android:text="@string/TipsTotal"
        android:textColor="@color/TextBlack"
        android:textSize="@dimen/textSize"
        android:textStyle="bold" />
 
    <TextView
        android:id="@+id/ResTotal"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/Total"
        android:text="@string/Dollar"
        android:textSize="@dimen/titres" />
 
    <Button
        android:id="@+id/raz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/RAZ" />
 
    <Button
        android:id="@+id/calcul"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/Calculer" />
 
    <TextView
        android:id="@+id/Niveau"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/seek"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/seek"
        android:layout_toRightOf="@+id/seek"
        android:ems="10"
        android:text="@string/Niveau"
        android:textSize="@dimen/textSize" />
 
 
</RelativeLayout>
 
</FrameLayout>
Seulement j'ai un léger soucis, lorsque je calcul mon résultat avec des champs vides EditMontantAddition et EditNombrePersonne, l'appli crash.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
01-20 22:42:48.310: E/AndroidRuntime(5429): java.lang.NumberFormatException: Invalid float: ""

Au niveau de la ligne 112, j'ai essayé le IF avec
Code : Sélectionner tout - Visualiser dans une fenêtre à part
if(EditMontantAddition.getText().length() = 0
ou avec
Code : Sélectionner tout - Visualiser dans une fenêtre à part
if((EditMontantAddition != null) && (!TextUtils.isEmpty(EditMontantAddition.getText().toString()))){
mais je n'y arrive pas.

1) Comment puis-je simplement tester si mes champs sont vides et afficher mon toast dans ce cas?

Une autre question au passage, je souhaiterai que ma seekbar aille de 10 à 20 (donc j'arrive à l'afficher ainsi avec le ligne 62

Code : Sélectionner tout - Visualiser dans une fenêtre à part
mProgressText.setText(progress + 10+ " " + getString(R.string.pourcentage));
mProgressText devient donc un string (exemple "15%" et je ne peux plus la manipuler en tant que float pour mes calculs suivants.

2) Comment puis-je afficher mon texte avec le string, et en même temps, affecter le float à une variable pour que je puisse le récupérer plus loin (lignes 102 à 123)

Merci d'avance