Android EditText & Dialogbox

362 views Asked by At

I've implemented an android screen with an Edittext. When the user touches this EditText, a datepickerdialog appears. This all worked fine, however my problem is that when the user touches the EditText, two datepickerdialogs are appearing. I only want one to appear.

I' wondering if anyone has encountered this problem, or is it only me? If yes, are there any solutions? I Google'd it, however found nothing :/

With regards to code, this is what I have:

        datebox = (TextView)findViewById(R.id.datebox);

    datebox.setOnTouchListener(new OnTouchListener() 

    {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            timedatedialog(false);
            return false;
        }});

public void timedatedialog(boolean flag) {

         if(flag==false){

               DatePickerDialog dateDlg = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                    int monthOfYear, int dayOfMonth)
            {
                Time chosenDate = new Time();
                chosenDate.set(dayOfMonth, monthOfYear, year);
                long dtDob = chosenDate.toMillis(true);
                CharSequence strDate = DateFormat.format("MMMM dd, yyyy", dtDob);

                Toast.makeText(ForecastingActivity.this,
                        "Date picked: " + strDate, Toast.LENGTH_SHORT).show();

                datebox.setText(strDate);

            }}, 2012,0, 1);

Any help is much appreciated. Thanks!

2

There are 2 answers

0
Jave On BEST ANSWER

Change your OnTouchListener to an OnClickListener or similiar. The onTouch()-method is invoked for any kind of touch event, i.e:
if you click the view you will go into onTouch() with a down-event,
and then if you happen move your finger the slightest you will go into onTouch() with a move-action, and finally you lift your finger and get onTouch() again, this time with a up-event.

The OnClickListener on the other hand only runs once, if you click the view.

0
user370305 On

Try this and let me know what happen..

 datebox.setOnTouchListener(new OnTouchListener() 
    {
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
         if((arg1.getAction() == MotionEvent.ACTION_DOWN))
          {
            timedatedialog(false);
          }
            return false;
        }});

Or just change to it onClickListener() method of your EditText..