如何清除TimePicker editText?

时间:2015-12-14 16:40:43

标签: android datetime android-edittext onactivityresult

活动B 中,我有两个可点击的editText(TI,TO)和一个保存Button。点击EditText后,系统会显示TimePicker dialog。保存Button用于将此两个值返回活动A。

现在的问题是EditText会在活动A 上显示来自TimePicker dialog的最新值,即使在活动B上没有选择任何值

我们在活动B 中说过,我在TI上输入了1:00,在TO中输入了2:00。第二次,我没有在活动B EditText上输入任何内容并按下按钮返回A,它仍然会在活动A上显示1:00和2:00。

活动B

      timeIn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { //shows timePick
                    Fragment fragDialog = getSupportFragmentManager().findFragmentByTag("TimePicker");
                    if (fragDialog == null) { // Fragment not added
                        tp.setFlag(TimePick.FLAG_START_DATE);
                        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                        tp.show(ft, "TimePicker");
                    } else {

                        // already active
                    }
                }
            });

            timeOut.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Fragment fragDialog = getSupportFragmentManager().findFragmentByTag("TimePicker");
                    if(fragDialog == null)
                    {  // Fragment not added
                        tp.setFlag(TimePick.FLAG_END_DATE);
                        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                        tp.show(ft, "TimePicker");
                    }
                    else
                    {
                        // already active
                    }
                }
            });

  public static class TimePick extends android.support.v4.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {

        public static final int FLAG_START_DATE = 00;
        public static final int FLAG_END_DATE = 01;
        private int flag = 00;

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);
            return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
        }

        public void setFlag(int i) {
            flag = i;
        }


        @Override
        public void onTimeSet(TimePicker view, int hourofDay, int minute) {

            if (flag == FLAG_START_DATE) {
                T1.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
                TimeIn = T1.getText().toString();
            }
            if (flag == FLAG_END_DATE) {
                TO.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
                TimeOut = TO.getText().toString();
            }

        }

  save.setOnClickListener(new View.OnClickListener()
        {  // return values to A activity
            @Override
            public void onClick(View v)
            {
                Intent returnIntent=new Intent();
                returnIntent.putExtra("TimeIn", TimeIn);
                returnIntent.putExtra("TimeOut",TimeOut);
                setResult(Activity.RESULT_OK,returnIntent);
                finish();

            }
        });

活动A

       @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
                if (resultCode == RESULT_OK) {
                    if (requestCode == PROJECT_REQUEST_CODE) {
                        ReceiveTimeIn = data.getStringExtra("TimeIn");
                        ReceiveTimeOut = data.getStringExtra("TimeOut");
   Toast.makeText(getApplicationContext(),ReceiveTimeIn+ReceiveTimeOut,Toast.LENGTH_LONG).show();
           }

Toast假设第二次显示null,但会显示1:00 2:00

已修改(活动B)

  save.setOnClickListener(new View.OnClickListener() { // return values to  activity A

                 @Override
                 public void onClick(View v)
                 {
                      if (isEdited == false)
                      {
                          if(T1!=null||TO!=null) // if editText not clicked but has value pass from ListView A
                          {
                              // return T1 and TO value to listView Activity A
                          }
                          else { // if editText not clicked and no value passed, then only null
                              TimeIn = "";
                              TimeOut = "";
                          }
                      }

                 Intent returnIntent = new Intent();
                 Project = project.getSelectedItem().toString();
                 Description = description.getText().toString();
                 progress = seekBar.getProgress();
                 returnIntent.putExtra("Project", Project);
                 returnIntent.putExtra("Description", Description);
                 returnIntent.putExtra("progress", progress);
                 returnIntent.putExtra("TimeIn", TimeIn);
                 returnIntent.putExtra("TimeOut", TimeOut);
                 setResult(Activity.RESULT_OK, returnIntent);
                 finish();
                 }
          });

1 个答案:

答案 0 :(得分:1)

After the findViewById of both timeIn and timeOut edittext set them empty as follows:

timeIn.setText(""); 
timeOut.setText(""); 

And your other two variable also:

TimeIn = "";
TimeOut = "";