Advertisement
builderman_build

Untitled

Nov 8th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1.  
  2. import com.example.radioconvert.R;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.text.Editable;
  6. import android.text.TextWatcher;
  7. import android.widget.EditText;
  8. import android.widget.RadioButton;
  9. import android.widget.RadioGroup;
  10. import android.widget.TextView;
  11.  
  12. public class MainActivity extends Activity {
  13. TextView mResult;
  14. EditText mToConvert;
  15. RadioGroup mRadioGroup;
  16. RadioButton mDollar, Meuro;
  17.  
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21.  
  22. mResult = (TextView) findViewById(R.id.result);
  23. mToConvert = (EditText) findViewById(R.id.toConvert);
  24. mRadioGroup = (RadioGroup) findViewById(R.id.radioG);
  25. mDollar = (RadioButton) findViewById(R.id.dollar);
  26. Meuro = (RadioButton) findViewById(R.id.euro);
  27.  
  28. mToConvert.addTextChangedListener(new TextWatcher() {
  29.  
  30. @Override
  31. public void onTextChanged(CharSequence arg0, int arg1, int arg2,
  32. int arg3) {
  33. // TODO Auto-generated method stub
  34.  
  35. }
  36.  
  37. @Override
  38. public void beforeTextChanged(CharSequence arg0, int arg1,
  39. int arg2, int arg3) {
  40. // TODO Auto-generated method stub
  41.  
  42. }
  43.  
  44. @Override
  45. public void afterTextChanged(Editable arg0) {
  46. convertCurrentAmount();
  47.  
  48. }
  49. });
  50. mRadioGroup
  51. .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  52.  
  53. @Override
  54. public void onCheckedChanged(RadioGroup group, int checkedId) {
  55. convertCurrentAmount();
  56. }
  57. });
  58. }
  59.  
  60. public void convertCurrentAmount() {
  61. double exchangeRate = -1;
  62. String exchangeSymbol = null;
  63. switch (mRadioGroup.getCheckedRadioButtonId()) {
  64. case R.id.dollar:
  65. mDollar.setChecked(true);
  66. Meuro.setChecked(false);
  67. exchangeRate = 3.76;
  68. exchangeSymbol = "$";
  69.  
  70.  
  71. break;
  72.  
  73. case R.id.euro:
  74. mDollar.setChecked(false);
  75. Meuro.setChecked(true);
  76. exchangeRate = 5;
  77. exchangeSymbol = "€";
  78.  
  79. break;
  80. }
  81. if (exchangeRate > 0 && exchangeSymbol != null) {
  82.  
  83. Double stringtoint = Double
  84. .valueOf(mToConvert.getText().toString());
  85. double result = stringtoint * exchangeRate;
  86. mResult.setText("" + exchangeSymbol + result);
  87.  
  88. }
  89. }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement