Obtain the time and minute values of a TimePickerDialog for use in another method

1

I have a button that when pressed it executes this method:

  private void mostrarHora(final TextView t) {
    final TimePickerDialog timePickerDialog = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
                 int  h= view.getCurrentHour();
                 int  m = view.getCurrentMinute();
                   setTxHora(h,m,t);
                }
}, h, m, false);
    timePickerDialog.setTitle("Selecciona la hora");
    timePickerDialog.show();
}

My question is simple: How can I use the values found in h and m in another method that is in the same class? How can I "extract" them, get them out of that method?

    
asked by Andry_UCI 05.03.2018 в 03:07
source

2 answers

0

You are already extracting them, you simply declare that these values will be stored in class variables so that you can use them in any method of your class.

Define the variables below the declaration of your class

public class MainActivity extends AppCompatActivity {

    private int m;
    private int h;

Now use these variables to store the data, so you can use the values of h and m

  private void mostrarHora(final TextView t) {
    final TimePickerDialog timePickerDialog = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
                 //int  h= view.getCurrentHour();
                 //int  m = view.getCurrentMinute();

                 h= view.getCurrentHour();
                 m = view.getCurrentMinute();

                   setTxHora(h,m,t);
                }
}, h, m, false);
    timePickerDialog.setTitle("Selecciona la hora");
    timePickerDialog.show();
}

It is important to mention that these variables will have a value of 0 until you open the TimePickerDialog and select a value!

    
answered by 05.03.2018 / 19:13
source
1

create two private int variables within your class, and set them from that method as follows:

private int hora = 0;
private int minuto = 0;

private void mostrarHora(final TextView t) {
    final TimePickerDialog timePickerDialog = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
                 int  h= view.getCurrentHour();
                 int  m = view.getCurrentMinute();
                 minuto = view.getCurrentMinute();
                 hora = view.getCurrentHour();
                   setTxHora(h,m,t);
                }
}, h, m, false);
    timePickerDialog.setTitle("Selecciona la hora");
    timePickerDialog.show();

}

    
answered by 05.03.2018 в 06:13