简体   繁体   English

当EditText失去焦点时,如何避免启动dialogfragment?

[英]How do I avoid launching dialogfragment when EditText loses focus?

I have a OnFocusChangeListener on an EditText line that launches a dialogfragment to allow the user to select a date from a Datepicker. 我在EditText行上有一个OnFocusChangeListener,它会启动一个dialogfragment,以允许用户从Datepicker中选择一个日期。 When I click "Set" on the DialogFragment, the selected date is set in the EditText. 当我在DialogFragment中单击“设置”时,所选日期将在EditText中设置。 Focus is on the EditText line with the cursor blinking at the beginning of the date that was set. 焦点位于EditText行上,光标在设置的日期开始时闪烁。

As soon as I click to another EditText line on the UI screen, the DialogFragment launches again, which I don't want. 当我单击UI屏幕上的另一个EditText行时,DialogFragment再次启动,这是我不希望的。 Basically I want to launch the DialogFragment when the EditText initially gains focus and after setting the date, I want the OnFocusChangeListener to ignore when the EditText loses focus as the user moves the cursor and focus to another EditText line on the screen. 基本上,我想在EditText最初获得焦点时启动DialogFragment,并在设置日期之后,当用户移动光标并将焦点移到屏幕上的另一个EditText行时,当EditText失去焦点时,我希望OnFocusChangeListener忽略。 Please advise. 请指教。

Here is partial code in Activity.java file: 这是Activity.java文件中的部分代码:

    ...
    fListenerEditText = (ListenerEditText) findViewById(R.id.FEditText);

    fListenerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getSupportFragmentManager(), "datePicker");
        }
    });

Here is partial code in DatePickerFragment.java file: 这是DatePickerFragment.java文件中的部分代码:

    ...
    // Puts the selected date into the EditText line.
    public void onDateSet(DatePicker view, int year, int month, int day) {
    txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
    String date=(month+1) + "/" + day + "/" + year;
    txtDate.setText(date);
    }

    // When the dialog date is set the dialogfragment closes and the softkeboard re-loads back on the CardViewActivity.
    public void onDismiss(final DialogInterface dialog) {
    super.onDismiss(dialog);
    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(CardViewActivity.INPUT_METHOD_SERVICE);
    // The soft keyboard always closes when the DialogFragment is displayed.  So the
    // line below toggles the soft keyboard to show again.
    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    }
  }

I believe you have forgot to put a check of "if editText having focus or not". 我相信您忘记了检查“ editText具有焦点”。 Do as following 做如下

  fListenerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
      if(hasFocus){
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
       }
      else {
         // do nothing
       }
    }
});

It will launch your DialogFragment only when editText gains focus and will do nothing focus lose. 仅当editText获得焦点时,它才会启动DialogFragment ,而失去焦点时则什么也不做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM