Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. package com.example.an02_gui_io_pignetl;
  2.  
  3. import androidx.appcompat.app.AlertDialog;
  4. import androidx.appcompat.app.AppCompatActivity;
  5.  
  6. import android.app.DatePickerDialog;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13. import java.text.Format;
  14. import java.text.ParseException;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Calendar;
  17. import java.util.Date;
  18. import java.util.Locale;
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22. private EditText editTextFirstName;
  23. private EditText editTextLastName;
  24. private EditText editTextBirthDate;
  25. private EditText editTextIncome;
  26.  
  27. private String firstname = "no name";
  28. private String lastname = "no name";
  29. private String birthdate = "0/0/00";
  30. private Double income = 0.0;
  31.  
  32. Format dateFormat;
  33. String pattern;
  34.  
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39.  
  40. editTextFirstName = findViewById(R.id.editTextFirstName);
  41. editTextLastName = findViewById(R.id.editTextLastName);
  42. editTextBirthDate = findViewById(R.id.editTextBirthDate);
  43. editTextIncome = findViewById(R.id.editTextIncome);
  44.  
  45. dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
  46. pattern = ((SimpleDateFormat) dateFormat).toLocalizedPattern();
  47. }
  48.  
  49. private void writeOutput(String message) {
  50. Log.d("MainActivity ", message + " " + firstname + " " + lastname + " " + birthdate + " " + income);
  51. }
  52.  
  53. public void onButtonSave_click(View v) {
  54.  
  55. String firstname = editTextFirstName.getText().toString();
  56. String lastname = editTextLastName.getText().toString();
  57. String birthdate = editTextBirthDate.getText().toString();
  58. String incomedouble = editTextIncome.getText().toString();
  59.  
  60.  
  61. try {
  62. if (firstname.equals("")) {
  63. new AlertDialog.Builder(this)
  64. .setMessage("Firstname may not be empty")
  65. .setNeutralButton("understood",null)
  66. .show();
  67. }
  68. else {
  69. if (lastname.equals("")) {
  70. new AlertDialog.Builder(this)
  71. .setMessage("Lastname may not be empty")
  72. .setNeutralButton("understood",null)
  73. .show();
  74. }
  75. else {
  76. if (birthdate.equals("")) {
  77. new AlertDialog.Builder(this)
  78. .setMessage("Birthdate may not be empty")
  79. .setNeutralButton("understood",null)
  80. .show();
  81. }
  82. else {
  83. if (incomedouble.equals("")) {
  84. new AlertDialog.Builder(this)
  85. .setMessage("Income may not be empty")
  86. .setNeutralButton("understood", null)
  87. .show();
  88. }
  89. else {
  90. Double income = Double.parseDouble(incomedouble);
  91.  
  92. Toast.makeText(this, "saved...", Toast.LENGTH_LONG).show();
  93.  
  94. this.firstname = firstname;
  95. this.lastname = lastname;
  96. this.birthdate = birthdate;
  97. this.income = income;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. catch(Exception e) {
  104. new AlertDialog.Builder(this)
  105. .setMessage("Something went wrong! The problem was: " + e)
  106. .setNeutralButton("understood", null)
  107. .show();
  108. }
  109. }
  110.  
  111. public void editTextBirthDate_click(View v) {
  112. Calendar c = parseDate(editTextBirthDate.getText().toString());
  113. int mYear = c.get(Calendar.YEAR);
  114. int mMonth = c.get(Calendar.MONTH);
  115. int mDay = c.get(Calendar.DAY_OF_MONTH);
  116. DatePickerDialog dialog = new DatePickerDialog(this, (view, year, monthOfYear, dayOfMonth) -> {
  117. Calendar c1 = Calendar.getInstance();
  118. c1.set(year, monthOfYear, dayOfMonth);
  119. Date myDate = c1.getTime();
  120. String dateStr = new SimpleDateFormat(pattern, Locale.getDefault()).format(myDate);
  121. editTextBirthDate.setText(dateStr);
  122. }, mYear, mMonth, mDay);
  123. dialog.show();
  124. }
  125.  
  126. private Calendar parseDate(String dateStr) {
  127. Calendar c = Calendar.getInstance();
  128. try {
  129. Date myDate = new SimpleDateFormat(pattern, Locale.getDefault()).parse(dateStr);
  130. c.setTime(myDate);
  131. }
  132. catch (ParseException e) {
  133. Toast.makeText(this, "No valid date entered!", Toast.LENGTH_LONG).show();
  134. }
  135. return c;
  136. }
  137.  
  138. @Override
  139. protected void onPause() {
  140. super.onPause();
  141. writeOutput("Activity paused");
  142. }
  143.  
  144. @Override
  145. protected void onStop() {
  146. super.onStop();
  147. writeOutput("Activity stopped");
  148. }
  149.  
  150. @Override
  151. protected void onSaveInstanceState(Bundle outState) {
  152. super.onSaveInstanceState(outState);
  153. outState.putString("FIRSTNAME",firstname);
  154. outState.putString("LASTNAME",lastname);
  155. outState.putString("MYDATE",birthdate);
  156. outState.putDouble("INCOME",income);
  157.  
  158. writeOutput("Activity state saved");
  159. }
  160.  
  161. @Override
  162. protected void onRestoreInstanceState(Bundle savedInstanceState) {
  163. super.onRestoreInstanceState(savedInstanceState);
  164. restoreInstanceState(savedInstanceState);
  165. }
  166.  
  167. private void restoreInstanceState(Bundle savedInstanceState) {
  168. if (savedInstanceState == null) {
  169. firstname = "no name";
  170. lastname = "no Name";
  171. birthdate = "0/0/00";
  172. income = 0.0;
  173. writeOutput("Activity state initialized");
  174. }
  175. else {
  176. firstname = savedInstanceState.getString("FIRSTNAME");
  177. lastname = savedInstanceState.getString("LASTNAME");
  178. birthdate = savedInstanceState.getString("MYDATE");
  179. income = savedInstanceState.getDouble("INCOME");
  180.  
  181. writeOutput("Activity state restored");
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement