Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.an02_gui_io_pignetl;
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.DatePickerDialog;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.text.Format;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Locale;
- public class MainActivity extends AppCompatActivity {
- private EditText editTextFirstName;
- private EditText editTextLastName;
- private EditText editTextBirthDate;
- private EditText editTextIncome;
- private String firstname = "no name";
- private String lastname = "no name";
- private String birthdate = "0/0/00";
- private Double income = 0.0;
- Format dateFormat;
- String pattern;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- editTextFirstName = findViewById(R.id.editTextFirstName);
- editTextLastName = findViewById(R.id.editTextLastName);
- editTextBirthDate = findViewById(R.id.editTextBirthDate);
- editTextIncome = findViewById(R.id.editTextIncome);
- dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
- pattern = ((SimpleDateFormat) dateFormat).toLocalizedPattern();
- }
- private void writeOutput(String message) {
- Log.d("MainActivity ", message + " " + firstname + " " + lastname + " " + birthdate + " " + income);
- }
- public void onButtonSave_click(View v) {
- String firstname = editTextFirstName.getText().toString();
- String lastname = editTextLastName.getText().toString();
- String birthdate = editTextBirthDate.getText().toString();
- String incomedouble = editTextIncome.getText().toString();
- try {
- if (firstname.equals("")) {
- new AlertDialog.Builder(this)
- .setMessage("Firstname may not be empty")
- .setNeutralButton("understood",null)
- .show();
- }
- else {
- if (lastname.equals("")) {
- new AlertDialog.Builder(this)
- .setMessage("Lastname may not be empty")
- .setNeutralButton("understood",null)
- .show();
- }
- else {
- if (birthdate.equals("")) {
- new AlertDialog.Builder(this)
- .setMessage("Birthdate may not be empty")
- .setNeutralButton("understood",null)
- .show();
- }
- else {
- if (incomedouble.equals("")) {
- new AlertDialog.Builder(this)
- .setMessage("Income may not be empty")
- .setNeutralButton("understood", null)
- .show();
- }
- else {
- Double income = Double.parseDouble(incomedouble);
- Toast.makeText(this, "saved...", Toast.LENGTH_LONG).show();
- this.firstname = firstname;
- this.lastname = lastname;
- this.birthdate = birthdate;
- this.income = income;
- }
- }
- }
- }
- }
- catch(Exception e) {
- new AlertDialog.Builder(this)
- .setMessage("Something went wrong! The problem was: " + e)
- .setNeutralButton("understood", null)
- .show();
- }
- }
- public void editTextBirthDate_click(View v) {
- Calendar c = parseDate(editTextBirthDate.getText().toString());
- int mYear = c.get(Calendar.YEAR);
- int mMonth = c.get(Calendar.MONTH);
- int mDay = c.get(Calendar.DAY_OF_MONTH);
- DatePickerDialog dialog = new DatePickerDialog(this, (view, year, monthOfYear, dayOfMonth) -> {
- Calendar c1 = Calendar.getInstance();
- c1.set(year, monthOfYear, dayOfMonth);
- Date myDate = c1.getTime();
- String dateStr = new SimpleDateFormat(pattern, Locale.getDefault()).format(myDate);
- editTextBirthDate.setText(dateStr);
- }, mYear, mMonth, mDay);
- dialog.show();
- }
- private Calendar parseDate(String dateStr) {
- Calendar c = Calendar.getInstance();
- try {
- Date myDate = new SimpleDateFormat(pattern, Locale.getDefault()).parse(dateStr);
- c.setTime(myDate);
- }
- catch (ParseException e) {
- Toast.makeText(this, "No valid date entered!", Toast.LENGTH_LONG).show();
- }
- return c;
- }
- @Override
- protected void onPause() {
- super.onPause();
- writeOutput("Activity paused");
- }
- @Override
- protected void onStop() {
- super.onStop();
- writeOutput("Activity stopped");
- }
- @Override
- protected void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- outState.putString("FIRSTNAME",firstname);
- outState.putString("LASTNAME",lastname);
- outState.putString("MYDATE",birthdate);
- outState.putDouble("INCOME",income);
- writeOutput("Activity state saved");
- }
- @Override
- protected void onRestoreInstanceState(Bundle savedInstanceState) {
- super.onRestoreInstanceState(savedInstanceState);
- restoreInstanceState(savedInstanceState);
- }
- private void restoreInstanceState(Bundle savedInstanceState) {
- if (savedInstanceState == null) {
- firstname = "no name";
- lastname = "no Name";
- birthdate = "0/0/00";
- income = 0.0;
- writeOutput("Activity state initialized");
- }
- else {
- firstname = savedInstanceState.getString("FIRSTNAME");
- lastname = savedInstanceState.getString("LASTNAME");
- birthdate = savedInstanceState.getString("MYDATE");
- income = savedInstanceState.getDouble("INCOME");
- writeOutput("Activity state restored");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement