Baixe o app para aproveitar ainda mais
Prévia do material em texto
package com.example.sala.myapplication; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.icu.text.SimpleDateFormat; import android.os.Bundle; import android.text.InputType; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import java.util.Calendar; import java.util.Locale; public class MainActivity extends Activity{ /** variaveis do projeto para os dias*/ private TextView pDisplayDate; private Button pPickDate; private int pYear; private int pMonth; private int pDay; /** variaveis do projeto para as horas*/ private TextView displayTime; private Button pickTime; private int pHour; private int pMinute; /** This integer will uniquely define the dialog to be used for displaying date picker.*/ static final int DATE_DIALOG_ID = 0; static final int TIME_DIALOG_ID=+1; /** Callback received when the user "picks" a date in the dialog */ private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { pYear = year; pMonth = monthOfYear; pDay = dayOfMonth; updateDisplay(); displayToastDate(); } }; /** Updates the time in the TextView */ private void updateDisplayTime() { displayTime.setText( new StringBuilder().append(pHour).append(":") .append(pMinute)); } private void displayTime(){ Toast.makeText(this, new StringBuilder().append("Hora selecionada é: "). append(displayTime.getText()), Toast.LENGTH_SHORT).show(); } /** Updates the date in the TextView */ private void updateDisplay() { pDisplayDate.setText( new StringBuilder() // Month is 0 based so add 1 .append(pDay).append("/") .append(pMonth + 1).append("/") .append(pYear).append(" ")); } /** Displays a notification when the date is update*/ private void displayToastDate() { Toast.makeText(this, new StringBuilder().append("Data selecionada é: ").append(pDisplayDate.getText()), Toast.LENGTH_SHORT).show(); } //método para a seleção da hora private TimePickerDialog.OnTimeSetListener mTimeSetListener= new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { pHour = hourOfDay; pMinute = minute; updateDisplayTime(); displayTime(); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** Capture our View elements */ pDisplayDate = (TextView) findViewById(R.id.displayDate); pPickDate = (Button) findViewById(R.id.pickDate); /** Listener for click event of the button */ pPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); displayTime=(TextView)findViewById(R.id.timeDisplay); pickTime=(Button)findViewById(R.id.pickTime); pickTime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showDialog(TIME_DIALOG_ID); } }); /** Get the current date */ final Calendar cal = Calendar.getInstance(); pYear = cal.get(Calendar.YEAR); pMonth = cal.get(Calendar.MONTH); pDay = cal.get(Calendar.DAY_OF_MONTH); /** Display the current date in the TextView */ updateDisplay(); displayToastDate(); final Calendar cals=Calendar.getInstance(); pHour=cals.get(Calendar.HOUR_OF_DAY); pMinute=cals.get(Calendar.MINUTE); updateDisplayTime(); displayTime(); } /** Create a new dialog for date picker */ @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay); case TIME_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, pHour, pMinute, false); } return null; } }
Compartilhar