2014-04-02 3 views
0

그것은 내 XML라는 이름의 파일 create_event.xml 여기방법 지정된 날짜 이후 날짜 선택기에서 날짜를 설정할 수

<!-- Start Date --> 
      <LinearLayout 
      style="@style/create_event_exc_margin_bottom" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <TextView 
       style="@style/create_event_exc_infotext_bold" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/start_date" /> 
      </LinearLayout> 

      <LinearLayout 
       style="@style/create_event_exc_margin_bottom" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 
       <EditText 
        style="@style/create_event_infotext_normal" 
        android:textColor="@android:color/black" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/extra_desc" 
        android:id="@+id/start_date"> 
       </EditText> 

       <Button 
        android:layout_height="wrap_content" 
        android:layout_weight="0" 
        android:id="@+id/btn_start_calender" 
        android:text="Calendar" 
        android:layout_width="100dp"> 
       </Button> 

      </LinearLayout> 

     <!-- Start Time -->  
       <LinearLayout 
      style="@style/create_event_exc_margin_bottom" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <TextView 
       style="@style/create_event_exc_infotext_bold" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/start_time" /> 
      </LinearLayout> 

      <LinearLayout 
       style="@style/create_event_exc_margin_bottom" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <EditText 
        style="@style/create_event_infotext_normal" 
        android:textColor="@android:color/black" 
        android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@drawable/extra_desc" 
      android:id="@+id/start_time"> 

     </EditText> 

    <Button 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:id="@+id/btn_start_timepicker" 
     android:text="Time Picker" 
     android:layout_width="100dp"></Button> 

       </LinearLayout> 

       <!-- End Date --> 
       <LinearLayout 
      style="@style/create_event_exc_margin_bottom" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <TextView 
       style="@style/create_event_exc_infotext_bold" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/end_date" /> 
      </LinearLayout> 

      <LinearLayout 
       style="@style/create_event_exc_margin_bottom" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <EditText 
        style="@style/create_event_infotext_normal" 
        android:textColor="@android:color/black" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/extra_desc" 
        android:id="@+id/end_date"> 
       </EditText> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_weight="0" 
        android:id="@+id/btn_end_calender" 
        android:text="Calendar" 
        android:layout_width="100dp"> 
       </Button> 
      </LinearLayout> 

       <!-- End Time --> 
       <LinearLayout 
      style="@style/create_event_exc_margin_bottom" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <TextView 
       style="@style/create_event_exc_infotext_bold" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/end_time" /> 
      </LinearLayout> 

      <LinearLayout 
       style="@style/create_event_exc_margin_bottom" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <EditText 
        style="@style/create_event_infotext_normal" 
        android:textColor="@android:color/black" 
        android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@drawable/extra_desc" 
      android:id="@+id/end_time"> 

     </EditText> 

    <Button 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:id="@+id/btn_end_timepicker" 
     android:text="Time Picker" 
     android:layout_width="100dp"></Button> 

       </LinearLayout> 

, 그것은 내 자바 파일 여기

public class Create_Event extends SherlockFragment implements OnClickListener { 

Button b_start_calender, b_end_calender, b_start_timepicker, b_end_timepicker; 
EditText start_date, start_time, end_date, end_time; 
private int start_year, start_month, start_day, start_hour, start_minute; 
private int end_year, end_month, end_day, end_hour, end_minute; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

b_start_calender=(Button)rootView.findViewById(R.id.btn_start_calender); 
    b_start_timepicker=(Button)rootView.findViewById(R.id.btn_start_timepicker); 
    b_end_calender=(Button)rootView.findViewById(R.id.btn_end_calender); 
    b_end_timepicker=(Button)rootView.findViewById(R.id.btn_end_timepicker); 

    start_date=(EditText)rootView.findViewById(R.id.start_date); 
    start_time=(EditText)rootView.findViewById(R.id.start_time); 
    end_date=(EditText)rootView.findViewById(R.id.end_date); 
    end_time=(EditText)rootView.findViewById(R.id.end_time); 

    b_start_calender.setOnClickListener(this); 
    b_start_timepicker.setOnClickListener(this); 
    b_end_calender.setOnClickListener(this); 
    b_end_timepicker.setOnClickListener(this); 

return rootView;   
} 


@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 


if(v==b_start_calender) { 
    final Calendar c=Calendar.getInstance(); 
    start_year=c.get(Calendar.YEAR); 
    start_month=c.get(Calendar.MONTH); 
    start_day=c.get(Calendar.DAY_OF_MONTH); 

    DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { 

     public void onDateSet(DatePicker view, int year, 
       int monthOfYear, int dayOfMonth) { 
startDate = Calendar.getInstance(); 
      startDate.set(Calendar.YEAR, year); 
      startDate.set(Calendar.MONTH, monthOfYear); 
      startDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
      start_date.setText(dayOfMonth + "-" 
        + (monthOfYear + 1) + "-" + year); 
     } 
    }; 
    DatePickerDialog d = new DatePickerDialog(getActivity(), 
      mDateSetListener, start_year, start_month, start_day); 
    d.show(); 
} 

if(v==b_start_timepicker) { 
    final Calendar c = Calendar.getInstance(); 
    start_hour=c.get(Calendar.HOUR_OF_DAY); 
    start_minute=c.get(Calendar.MINUTE); 

    TimePickerDialog.OnTimeSetListener mTimeSetListner = new TimePickerDialog.OnTimeSetListener() { 

     @Override 
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
      start_time.setText(hourOfDay + ":" + minute); 
     } 
    }; 
    TimePickerDialog t=new TimePickerDialog(getActivity(), mTimeSetListner, start_hour, start_minute,false); 
    t.show(); 
} 

if(v==b_end_calender) { 
    final Calendar c=Calendar.getInstance(); 
    end_year=c.get(Calendar.YEAR); 
    end_month=c.get(Calendar.MONTH); 
    end_day=c.get(Calendar.DAY_OF_MONTH); 

    DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { 
     public void onDateSet(DatePicker view, int year, 
       int monthOfYear, int dayOfMonth) { 
Calendar newEndDate=Calendar.getInstance(); 
      newEndDate.set(Calendar.YEAR, year); 
      newEndDate.set(Calendar.MONTH, monthOfYear); 
      newEndDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); 



      if(newEndDate.before(startDate)) 
      { 
       end_date.setText("Plz.. enter date after starting date.."); 

      } 

      else 
      { 

       end_date.setText(dayOfMonth + "-" 
         + (monthOfYear + 1) + "-" + year); 

      } 


     } 
    }; 
    DatePickerDialog d = new DatePickerDialog(getActivity(), 
      mDateSetListener, end_year, end_month, end_day); 
    d.show();  
} 


if(v==b_end_timepicker) { 
    final Calendar c = Calendar.getInstance(); 
    end_hour=c.get(Calendar.HOUR_OF_DAY); 
    end_minute=c.get(Calendar.MINUTE); 

    TimePickerDialog.OnTimeSetListener mTimeSetListner = new TimePickerDialog.OnTimeSetListener() { 

     @Override 
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
      end_time.setText(hourOfDay + ":" + minute); 
     } 
    }; 
    TimePickerDialog t=new TimePickerDialog(getActivity(), mTimeSetListner, end_hour, end_minute,false); 
    t.show(); 
} 
} 

이다, 시작 날짜 및 종료 날짜 수 제대로 설정하십시오. 종료일은 시작일 이전에 설정할 수 없습니다. 그러나 끝 날짜를 먼저 설정하고 시작 날짜를 설정하면 ...이 경우 제대로 작동하지 않습니다. 이 사건을 처리하는 방법 ??? 나는 또한 시작과 끝 날짜가 같을 수 없으며 사용자가 오늘 날짜 이전에 날짜를 선택할 수 없도록해야합니다 ... 조건을 넣을 위치는 어디입니까 ?? 어떻게해야합니까 ?? 제발 도와주세요 ...

답변

0

다음과 같이 끝 날짜 수신기를 변경합니다.

if(v==b_end_calender) { 
    final Calendar c=Calendar.getInstance(); 
    end_year=c.get(Calendar.YEAR); 
    end_month=c.get(Calendar.MONTH); 
    end_day=c.get(Calendar.DAY_OF_MONTH); 

    DatePickerDialog.OnDateSetListener mDateSetListener 
     = new DatePickerDialog.OnDateSetListener() { 
      public void onDateSet(DatePicker view, 
       int year, int monthOfYear, int dayOfMonth) { 
       Calendar newEndDate = Calendar.getInstance(); 
       newEndDate.set(Calendar.YEAR, year); 
       newEndDate.set(Calendar.MONTH, monthOfYear); 
       newEndDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); 

       if(newEndDate.before(startDate)) { 
        // handle the error 
       } else { 
       end_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); 
       } 

      } 
    }; 

    new DatePickerDialog(getActivity(), 
     mDateSetListener, end_year, end_month, end_day).show(); 

} 당신이 발견하는 경우

, 내가 전역 변수 startDate을 사용하고 있습니다. 선택 될 때 전역 적으로 저장해야합니다.

Calendar startDate = Calendar.getInstance(); // global variable 

if(v==b_start_calender) { 
    final Calendar c=Calendar.getInstance(); 
    start_year=c.get(Calendar.YEAR); 
    start_month=c.get(Calendar.MONTH); 
    start_day=c.get(Calendar.DAY_OF_MONTH); 

    DatePickerDialog.OnDateSetListener mDateSetListener 
     = new DatePickerDialog.OnDateSetListener() { 
      public void onDateSet(DatePicker view, 
       int year, int monthOfYear, int dayOfMonth) { 
        startDate.set(Calendar.YEAR, year); 
        startDate.set(Calendar.MONTH, monthOfYear); 
        startDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
        start_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); 
     } 
    }; 

    new DatePickerDialog(getActivity(), 
     mDateSetListener, start_year, start_month, start_day).show(); 
} 

두 번째로, DateFormatter 클래스를 살펴보면, 나는 그들이 당신에게 유용 할 것이라고 확신합니다.

건배.

+0

endDate 이후에도 startDate를 방지하려는 경우에도 endDate 전역 변수를 사용할 수도 있습니다. – tambykojak

+0

예, 작동 중입니다 .... 지식을 공유해 주셔서 대단히 감사드립니다 .... 사실 저는 startDate를 전역 변수로 선언하지 않았습니다 ... 이것이 작동하지 않는 이유입니다 ... – user3388240