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));
}
}
}
}
};
} |
Partager