0

두 개의 하단 탭이 포함 된 탭 활동이 있습니다. FrameLayout은 제가 만든 특수한 레이아웃 파일로 채워져 있습니다. 이 두 레이아웃 파일은 Fragment 클래스를 확장하는 Java 클래스에 의해 실행됩니다. 이것은 내가 지금 가지고있는 것에 대한 간단한 설명입니다.DialogFragment는 하나가 아닌 몇 개의 대화 상자 창을 만듭니다.

이제 문제에 대한 세부 정보가 제공됩니다. 레이아웃 파일에는 클릭 할 때 DatePickerDialog를 만들어야하는 두 개의 EditText 필드가 있습니다. DialogFragment 클래스를 사용하여 대화 상자를 구현했습니다. 하나는 두 텍스트 필드 모두에 사용됩니다. Hovewer, edittext를 클릭하면 여러 개의 DatePicker 대화 상자가 생성되지만 하나가 아닌 여러 논리가 뒤 따른다는 것이 명확하지 않습니다. 2, 3 또는 4 개의 대화 상자 창을 생성 할 수 있습니다.

이 문제를 어떻게 해결할 수 있습니까?

활동 tabView 클래스

public class DaysTabView extends Fragment { 

/* 
* some code was not included to make easir to read 
*/ 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.days_tab_view, container, false); 
     mFromDate = (EditText) v.findViewById(R.id.fromDate); 

     mFromDate.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       mFromDate.setText(""); 
       SetDateToDefault(1);  //some method 
       Bundle args = new Bundle(); 
       args.putInt("day", day1); 
       args.putInt("month", month1); 
       args.putInt("year", year1); 
       myDatePickerDialogFragment d1 = new myDatePickerDialogFragment(); 
       d1.setArguments(args); 
       d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question 
       d1.show(getFragmentManager(), "days"); 
       return false; 
      } 
     }); 
    } 
} 

public class Activity extends AppCompatActivity { 

    FragmentTabHost mFragmentTabHost; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_quick_calculation); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
     mFragmentTabHost.setup(QuickCalculationActivity.this, getSupportFragmentManager(), android.R.id.tabcontent); 

     mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec("days").setIndicator("DAYS"), DaysTabView.class, null); 
     mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec("date").setIndicator("DATE"), DateTabView.class, null); 
    } 
} 

하나 setOnTouchListener 대신 거짓에

public class myDatePickerDialogFragment extends DialogFragment{ 

    int mDay, mMonth, mYear; 
    OnDateSetListener onSetDate; 

    public myDatePickerDialogFragment(){ 
    } 

    @Override 
    public void setArguments(Bundle args) { 
     super.setArguments(args); 
     mDay = args.getInt("day"); 
     mMonth = args.getInt("month"); 
     mYear = args.getInt("year"); 
    } 

    public void setOnDateSetListener(OnDateSetListener setDate){ 
     onSetDate = setDate; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     return new DatePickerDialog(getActivity(), onSetDate, mYear, mMonth, mDay); 
    } 
} 

답변

1

. 그 이유는 터치 리스너가 이벤트가 발생할 때마다 (손가락 내림, 손가락 움직임, 손가락 업 등) 트리거되는 반면 클릭 리스너는 한 번만 트리거됩니다.

는 그래서 DaysTabView 클래스의 변경이로 :

mFromDate.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public boolean onClick(View v) { 
      mFromDate.setText(""); 
      SetDateToDefault(1);  //some method 
      Bundle args = new Bundle(); 
      args.putInt("day", day1); 
      args.putInt("month", month1); 
      args.putInt("year", year1); 
      myDatePickerDialogFragment d1 = new myDatePickerDialogFragment(); 
      d1.setArguments(args); 
      d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question 
      d1.show(getFragmentManager(), "days"); 
      return false; 
     } 
    }); 
1

반환 진정한 DialogFragment 클래스 : 여기 내 코드입니다.

if(event.getAction() == MotionEvent.ACTION_UP){ 

      // Do what you want 
      return true; 
     } 
     return false; 
} 

처럼 그것은해야한다 : 나는 당신의 EditTexts에 onClickListeneronTouchListener을 변경하는 것이 좋습니다 것

mFromDate.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
      if(event.getAction() == MotionEvent.ACTION_UP){ 

       mFromDate.setText(""); 
       SetDateToDefault(1);  //some method 
       Bundle args = new Bundle(); 
       args.putInt("day", day1); 
       args.putInt("month", month1); 
       args.putInt("year", year1); 
       myDatePickerDialogFragment d1 = new myDatePickerDialogFragment(); 
       d1.setArguments(args); 
       d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question 
       d1.show(getFragmentManager(), "days"); 
       return true; 
      } 
      return false; 
    } 

     }); 
1
onTouchListener 코드 변경

mFromDate.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      final int actionPeformed = ev.getAction(); 
      switch(actionPeformed){ 
      case MotionEvent.ACTION_UP:{ 

      mFromDate.setText(""); 
      SetDateToDefault(1);  //some method 
      Bundle args = new Bundle(); 
      args.putInt("day", day1); 
      args.putInt("month", month1); 
      args.putInt("year", year1); 
      myDatePickerDialogFragment d1 = new myDatePickerDialogFragment(); 
      d1.setArguments(args); 
      d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question 
      d1.show(getFragmentManager(), "days"); 
      break; 
    } 

    return true; 

    } 
}); 
1

어쩌면 대신 TouchListener

의에 ClickListener를 사용해야합니다