Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android.todolista;
- import android.app.DatePickerDialog;
- import android.app.Dialog;
- import android.app.DialogFragment;
- import android.app.TimePickerDialog;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.text.format.DateFormat;
- import android.view.View;
- import android.widget.Button;
- import android.widget.DatePicker;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.TextView;
- import android.widget.TimePicker;
- import android.widget.Toast;
- import java.util.Calendar;
- public class MainActivity extends AppCompatActivity {
- private EditText etItemDesc;
- private static TextView tvDate;
- private static TextView tvTime;
- private Button btnChooseDate;
- private Button btnChooseTime;
- private Button btnAdd;
- private Button btnCancel;
- private RadioButton rbtnDone;
- public static class TimePickerFragment extends DialogFragment
- implements TimePickerDialog.OnTimeSetListener {
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- // Use the current time as the default values for the picker
- final Calendar c = Calendar.getInstance();
- int hour = c.get(Calendar.HOUR_OF_DAY);
- int minute = c.get(Calendar.MINUTE);
- // Create a new instance of TimePickerDialog and return it
- return new TimePickerDialog(getActivity(), this, hour, minute,
- DateFormat.is24HourFormat(getActivity()));
- }
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
- tvTime.setText(hourOfDay+":"+minute);
- }
- }
- public static class DatePickerFragment extends DialogFragment
- implements DatePickerDialog.OnDateSetListener {
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- // Use the current date as the default date in the picker
- final Calendar c = Calendar.getInstance();
- int year = c.get(Calendar.YEAR);
- int month = c.get(Calendar.MONTH);
- int day = c.get(Calendar.DAY_OF_MONTH);
- // Create a new instance of DatePickerDialog and return it
- return new DatePickerDialog(getActivity(), this, year, month, day);
- }
- public void onDateSet(DatePicker view, int year, int month, int day) {
- tvDate.setText(day+"."+month+"."+year);
- }
- }
- public void addButtonClick(View v) {
- Toast.makeText(this, etItemDesc.getText() + ", " + tvDate.getText()
- + ", " + tvTime.getText() + ", " + rbtnDone.isChecked(), Toast.LENGTH_SHORT).show();
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etItemDesc = (EditText) findViewById(R.id.etItemDesc);
- rbtnDone = (RadioButton) findViewById(R.id.radioButton);
- tvDate = (TextView) findViewById(R.id.tvDate);
- tvTime = (TextView) findViewById(R.id.tvTime);
- btnChooseDate = (Button) findViewById(R.id.btnChooseDate);
- btnChooseTime = (Button) findViewById(R.id.btnChooseTime);
- btnAdd = (Button) findViewById(R.id.btnAdd);
- btnCancel = (Button) findViewById(R.id.btnCancel);
- btnChooseDate.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- DialogFragment newFragment = new DatePickerFragment();
- newFragment.show(getFragmentManager(), "datePicker");
- }
- });
- btnChooseTime.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- DialogFragment newFragment = new TimePickerFragment();
- newFragment.show(getFragmentManager(), "timePicker");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement