Two timePicker s a button qdo clicked more than once arrow pro ediText wrong

Asked

Viewed 67 times

1

I want to click the first button and return the hour 1 and the second the hour 2 but when I click again on 1 it inserts into 2

Most of the code is credited to this website

public class MainActivity extends Activity {

    private TextView tvDisplayDate, tvDisplayDate2;
    private TimePicker dpResult;
    private Button btnChangeDate, btnChangeDate2, bt1;
    private EditText edt1, edt2;
    private TimePicker timePicker1;
    private int year;
    private int month;
    private int day;

    //meu
    private int hora;
    private int min;

    static final int DATE_DIALOG_ID = 1;
    static final int DATE_DIALOG_ID2 = 2;
    static final int DATE_DIALOG_ID3 = 3; //meu no switch
    int cur = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setCurrentDateOnView();
        addListenerOnButton();
    }

    // display current date
    public void setCurrentDateOnView() {
        tvDisplayDate = (TextView) findViewById(R.id.tvDate);
        tvDisplayDate2 = (TextView) findViewById(R.id.tvDate2);

        edt1 = (EditText) findViewById(R.id.edt1);
        edt2 = (EditText) findViewById(R.id.edt2);

        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        //meu
        hora = c.get(Calendar.HOUR_OF_DAY);
        min = c.get(Calendar.MINUTE);

       //  set current date into textview
        tvDisplayDate.setText(
                new StringBuilder().append((hora))
                       .append(":").append((min)));

        tvDisplayDate2.setText(tvDisplayDate.getText().toString());

        edt1.setText(new StringBuilder().append(hora).append(":").append(min).append(" "));
        //meu
    }

    public void addListenerOnButton() {
        bt1 = (Button) findViewById(R.id.bt1);
        bt1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID3);
            }
        });

        btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
        btnChangeDate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }

        });

        btnChangeDate2 = (Button) findViewById(R.id.btnChangeDate2);
        btnChangeDate2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID2);
            }
        });
        ////até aqui meu
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                cur = DATE_DIALOG_ID;
                // set date picker as current date
                return new TimePickerDialog(this, timePickerListener, hora, min, false);
            case DATE_DIALOG_ID2:
                cur = DATE_DIALOG_ID2;
                System.out.println("onCreateDialog2  : " + id);
                // set date picker as current date
                return new TimePickerDialog(this, timePickerListener, hora, min, false);
        }

        return null;
    }

    private TimePickerDialog.OnTimeSetListener timePickerListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int selectedHour,
                                      int selectedMinute) {
                    hora = selectedHour;
                    min = selectedMinute;

                    if(cur == DATE_DIALOG_ID){
                        // set selected date into textview
                        tvDisplayDate.setText("Time1 : " + new StringBuilder().append((hora ))
                                .append("-").append(min).append(":")
                                .append(" "));
                    } else {
                        if (cur == DATE_DIALOG_ID2) {
                            tvDisplayDate2.setText("Time2 : " + new StringBuilder().append((hora))
                                    .append("-").append(min).append(":")
                                    .append(" "));
                        }
                    }
                }
            };

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }
}
  • Sumidoghost, I formatted the code because it was very difficult to follow the logic. I think I missed including the function showDialog. Looking around I see no error in this code.

  • Thanks, I never used the stack, I’ll check.

  • I did not find the error, the example above the site link, is a datepicker with the same error.

  • Seeing now, the method showDialog is of the class itself Activity and is in disuse. I recommend you use a Dialog directly or a Dialogfragment. Or call directly onCreateDialog(...).show();

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.