Advertisement
ridjis

MainActivity

Dec 2nd, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. package com.example.android.todolista;
  2.  
  3. import android.app.DatePickerDialog;
  4. import android.app.Dialog;
  5. import android.app.DialogFragment;
  6. import android.app.TimePickerDialog;
  7. import android.os.Bundle;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.text.format.DateFormat;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.DatePicker;
  13. import android.widget.EditText;
  14. import android.widget.RadioButton;
  15. import android.widget.TextView;
  16. import android.widget.TimePicker;
  17. import android.widget.Toast;
  18.  
  19. import java.util.Calendar;
  20.  
  21. public class MainActivity extends AppCompatActivity {
  22.     private EditText etItemDesc;
  23.     private static TextView tvDate;
  24.     private static TextView tvTime;
  25.     private Button btnChooseDate;
  26.     private Button btnChooseTime;
  27.     private Button btnAdd;
  28.     private Button btnCancel;
  29.     private RadioButton rbtnDone;
  30.  
  31.     public static class TimePickerFragment extends DialogFragment
  32.             implements TimePickerDialog.OnTimeSetListener {
  33.  
  34.         @Override
  35.         public Dialog onCreateDialog(Bundle savedInstanceState) {
  36.             // Use the current time as the default values for the picker
  37.             final Calendar c = Calendar.getInstance();
  38.             int hour = c.get(Calendar.HOUR_OF_DAY);
  39.             int minute = c.get(Calendar.MINUTE);
  40.  
  41.             // Create a new instance of TimePickerDialog and return it
  42.             return new TimePickerDialog(getActivity(), this, hour, minute,
  43.                     DateFormat.is24HourFormat(getActivity()));
  44.         }
  45.  
  46.         public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  47.             tvTime.setText(hourOfDay+":"+minute);
  48.         }
  49.     }
  50.  
  51.     public static class DatePickerFragment extends DialogFragment
  52.             implements DatePickerDialog.OnDateSetListener {
  53.  
  54.         @Override
  55.         public Dialog onCreateDialog(Bundle savedInstanceState) {
  56.             // Use the current date as the default date in the picker
  57.             final Calendar c = Calendar.getInstance();
  58.             int year = c.get(Calendar.YEAR);
  59.             int month = c.get(Calendar.MONTH);
  60.             int day = c.get(Calendar.DAY_OF_MONTH);
  61.  
  62.             // Create a new instance of DatePickerDialog and return it
  63.             return new DatePickerDialog(getActivity(), this, year, month, day);
  64.         }
  65.  
  66.         public void onDateSet(DatePicker view, int year, int month, int day) {
  67.             tvDate.setText(day+"."+month+"."+year);
  68.         }
  69.     }
  70.  
  71.     public void addButtonClick(View v) {
  72.         Toast.makeText(this, etItemDesc.getText() + ", " + tvDate.getText()
  73.                 + ", " + tvTime.getText() + ", " + rbtnDone.isChecked(), Toast.LENGTH_SHORT).show();
  74.     }
  75.  
  76.     @Override
  77.     protected void onCreate(Bundle savedInstanceState) {
  78.         super.onCreate(savedInstanceState);
  79.         setContentView(R.layout.activity_main);
  80.  
  81.         etItemDesc = (EditText) findViewById(R.id.etItemDesc);
  82.         rbtnDone = (RadioButton) findViewById(R.id.radioButton);
  83.         tvDate = (TextView) findViewById(R.id.tvDate);
  84.         tvTime = (TextView) findViewById(R.id.tvTime);
  85.         btnChooseDate = (Button) findViewById(R.id.btnChooseDate);
  86.         btnChooseTime = (Button) findViewById(R.id.btnChooseTime);
  87.         btnAdd = (Button) findViewById(R.id.btnAdd);
  88.         btnCancel = (Button) findViewById(R.id.btnCancel);
  89.  
  90.         btnChooseDate.setOnClickListener(new View.OnClickListener(){
  91.             @Override
  92.             public void onClick(View v) {
  93.                 DialogFragment newFragment = new DatePickerFragment();
  94.                 newFragment.show(getFragmentManager(), "datePicker");
  95.             }
  96.         });
  97.  
  98.         btnChooseTime.setOnClickListener(new View.OnClickListener() {
  99.             @Override
  100.             public void onClick(View v) {
  101.                 DialogFragment newFragment = new TimePickerFragment();
  102.                 newFragment.show(getFragmentManager(), "timePicker");
  103.             }
  104.         });
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement