Bonjour,
Je développe une appli dans laquelle je souhaite réaliser des conversions d'unité "à la volée" : dans un EditText je saisis la température (par ex) en Celsius et dans le second j'obtiens la température en Farhenheit. Inversement, si je saisis une température en °F dans l'EditText °F , j'obtiens la température en °C.
J'ai concaténé des bouts de codes : voir ci dessous.
Ça fonctionne à peu près (j'ai un souci avec le signe de la valeur numérique) mais ça ne gère que DEUX EditText ... et je souhaiterais en gérer une 20taine (10 couples) !
Un ou une "senior" du Java aurait-il ou aurait-elle une idée pour éviter la solution lourdingue du copier-coller-aménager 10 fois ce code !
Bien @ vous!
bp


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
 
 
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
import java.text.DecimalFormat;
 
public class MainActivity extends Activity {
    private EditText celsius;
    private EditText fahrenheit;
    private String valeur_aff;
    private double valeur_num ;
    boolean flag_fahrenheit;
    boolean flag_celsius;
    DecimalFormat df_2d = new DecimalFormat("0.00");
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /* Initializing views */
        celsius = (EditText) findViewById(R.id.C);
        fahrenheit = (EditText) findViewById(R.id.F);
        /* Set Text Watcher listener */
        celsius.addTextChangedListener(Watcher_celsius);
        fahrenheit.addTextChangedListener(Watcher_fahrenheit);
 
        fahrenheit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    Toast.makeText(getApplicationContext(), "fahrenheit Got the focus", Toast.LENGTH_LONG).show();
                    flag_fahrenheit=true;
                } else {
                    Toast.makeText(getApplicationContext(), "fahrenheit Lost the focus", Toast.LENGTH_LONG).show();
                    flag_fahrenheit=false;
                }
            }
        });
        celsius.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    Toast.makeText(getApplicationContext(), "celsius Got the focus", Toast.LENGTH_LONG).show();
                    flag_celsius=true;
                } else {
                    Toast.makeText(getApplicationContext(), "celsius Lost the focus", Toast.LENGTH_LONG).show();
                    flag_celsius=false;
                }
            }
        });
    }
 
 
    private final TextWatcher Watcher_fahrenheit = new TextWatcher() {
 
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
 
        public void afterTextChanged(Editable s) {
            if ( flag_fahrenheit) {
                if (s.length() == 0) {
                } else {
                    valeur_aff = fahrenheit.getEditableText().toString();
                    if (valeur_aff.equals("")) {
                        valeur_aff = "0";
                    }
                    if (valeur_aff.equals("-")) {
                        valeur_aff = "-";
                        celsius.setText("-");
                    }
                    if (!valeur_aff.equals("") && !valeur_aff.equals("-")) {
                        valeur_num = Double.parseDouble(valeur_aff);
                        valeur_num = (valeur_num - 32d) * 5d / 9d;
                        celsius.setText(df_2d.format(valeur_num));
                    }
                }
            }
 
        }
    };
 
 
 
    private final TextWatcher Watcher_celsius = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
 
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
 
        public void afterTextChanged(Editable s) {
            if (flag_celsius) {
                if (s.length() == 0) {
                } else {
                    valeur_aff = celsius.getEditableText().toString();
                    if (valeur_aff.equals("")) {
                        valeur_aff = "0";
                    }
                    if (valeur_aff.equals("-")) {
                        valeur_aff = "-";
                        fahrenheit.setText("-");
                    }
                    if (!valeur_aff.equals("") && !valeur_aff.equals("-")) {
                        valeur_num = Double.parseDouble(valeur_aff);
                        valeur_num = valeur_num * 9d / 5d + 32d;
                        fahrenheit.setText(df_2d.format(valeur_num));
                    }
                }
            }
        }
 
    };
 
 
 
 
 
 
}
et le xml pour ceusses qui veulent tester :
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
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp">
 
 
    <EditText
        android:id="@+id/C"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:padding="12dp">
 
        <!--    <requestFocus />-->
    </EditText>
    <EditText
        android:id="@+id/F"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:padding="12dp">
 
        <!--    <requestFocus />-->
    </EditText>
 
    <!--   <TextView
          android:id="@+id/F"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/password"
          android:background="#ffd33a26"
          android:padding="5dp"
          android:text="* Not Entered"
          android:textColor="#fff" />-->
</LinearLayout>