2015-03-27 2 views
0

Я реализовал CalendarView в моем проекте, и я хотел бы получить DD/MM/формат YY щелкать датуCalendarView в Android

Я попытался следующие:

calendar =(CalendarView) findViewById(R.id.calendarforstart); 
calendar.setShowWeekNumber(false); 
    calendar.setFirstDayOfWeek(2); 
    calendar.setOnDateChangeListener(new OnDateChangeListener() 
    { 
     @Override 
     public void onSelectedDayChange(CalendarView view, int year, int month, int day) 
     { 
      Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show(); 
     } 
    }); 

выше работает отлично но если я прокручу календарь до следующего месяца или предыдущего месяца. Я получаю тост, который я не хочу вместо этого, я хотел бы получить тост только тогда, когда я нажимаю на дату?

Как это реализовать?

Дайте мне знать!

Спасибо!

+0

Можете ли вы использовать длинную переменную live 'private long lastDate = 0;' для хранения статуса даты? – tianyu

+0

Этот [Android: CalendarView OnDateChangeLIstener] (http://stackoverflow.com/questions/12641250/android-calendarview-ondatechangelistener) может вам очень помочь. – tianyu

+0

@tianyu: Делая это, я не могу выбрать текущую дату. :(Как сделать это? – TheDevMan

ответ

1

Решение cannot select the current date - это трудный путь. Идея заключается в сохранении позиции экрана каждого элемента списка и проверке каждого dispatchTouchEvent, если (x, y) находится внутри элемента просмотра даты.

private long lastDate = 0; 

private int downX; 
private int downY; 

private int upX; 
private int upY; 

private int lastX=0; 
private int lastY=0; 

private ItemArea currentArea; 

private int year = -1; 
private int month; 
private int day; 

private ListView listView; 

private int[] startPoint = new int[2]; 

private int listItemCount = 0; 
private int listItemWidth = 0; 
private int listItemHeight = 0; 

ArrayList<ItemArea> areaList = new ArrayList<>(); 

private CalendarView calendar; 

private boolean isInitialized = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    calendar =(CalendarView) findViewById(R.id.calendarforstart); 
    calendar.setShowWeekNumber(false); 
    calendar.setFirstDayOfWeek(2); 
    calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { 
     @Override 
     public void onSelectedDayChange(CalendarView view, int year, int month, int day) { 
      if(view.getDate() != lastDate) { 
       lastDate = view.getDate(); 
       MainActivity.this.year = year; 
       MainActivity.this.month = month; 
       MainActivity.this.day = day; 
       makeToast(); 
       currentArea = getItemArea(upX, upY); 
       isInitialized = true; 
      } 
     } 
    }); 

    listView = (ListView)calendar.findViewById(android.R.id.list); 

} 

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 

    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      downX = (int)event.getX(); 
      downY = (int)event.getY(); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      break; 
     case MotionEvent.ACTION_UP: 
      upX = (int)event.getX(); 
      upY = (int)event.getY(); 

      if (areaList.size()==0) { 
       generateList(); 
      } 

      // make sure it's not move 
      if (Math.abs(downX-upX)<3 && Math.abs(downY-upY)<3) { 
       ItemArea area = getItemArea(upX, upY); 

       // on application start up and click the current date, there are stored status. 
       if (currentArea==null || !isInitialized) { 
        long time = calendar.getDate(); 
        Calendar currentCalendar = new GregorianCalendar(); 
        currentCalendar.setTimeInMillis(time); 
        year = currentCalendar.get(Calendar.YEAR); 
        month = currentCalendar.get(Calendar.MONTH); 
        day = currentCalendar.get(Calendar.DAY_OF_MONTH); 
        makeToast(); 
       } 

       if (area!=null && area.equals(currentArea)) { 
        makeToast(); 
       } 
      } else { 
       // FIXME: still have bug when drag/scroll back 
       // it's move event, list view will scroll up/down, and update the y 
       if (currentArea!=null) { 
        if (downY<upY) { 
         // move down 
         int times = (upY-downY)/listItemHeight; 
         currentArea.top+=listItemHeight*(times+1); 
         currentArea.bottom+=listItemHeight*(times+1); 
        } else { 
         // move up 
         int times = (downY-upY)/listItemHeight; 
         currentArea.top-=listItemHeight*(times+1); 
         currentArea.bottom-=listItemHeight*(times+1); 
        } 
       } 
      } 
      break; 
    } 

    return super.dispatchTouchEvent(event); 
} 

private void generateList() { 
    listItemCount = listView.getChildCount(); 
    listItemHeight = listView.getChildAt(0).getHeight(); 
    listItemWidth = listView.getChildAt(0).getWidth(); 
    listView.getChildAt(0).getLocationOnScreen(startPoint); 

    int deltaX = (int)(listItemWidth/7.0); 

    for (int i=0; i< listItemCount; i++) { 
     for (int j=0; j<7; j++) { 
      areaList.add(new ItemArea(startPoint[0]+deltaX*j, startPoint[1]+listItemHeight*i, 
        startPoint[0]+deltaX*(j+1), startPoint[1]+listItemHeight*(i+1))); 
     } 

    } 

} 

private void makeToast() { 
    Log.d("TAG", "do your job here"); 
    lastX = upX; 
    lastY = upY; 

    Toast.makeText(this, "" + day + "/" + month + "/" + year, Toast.LENGTH_LONG).show(); 
} 

private ItemArea getItemArea(int x, int y) { 
    for (int i=0; i < areaList.size(); i++) { 
     if (areaList.get(i).contains(x, y)) { 
      return areaList.get(i); 
     } 
    } 
    return null; 
} 

private class ItemArea { 
    int left; 
    int top; 
    int right; 
    int bottom; 

    ItemArea(int left, int top, int right, int bottom) { 
     this.left = left; 
     this.top = top; 
     this.right = right; 
     this.bottom = bottom; 
    } 

    boolean contains(int x, int y) { 
     return x>=left && x<=right && y>=top && y<=bottom; 
    } 

    boolean equals(ItemArea area) { 
     return area!=null && 
       this.right==area.right && 
       this.left==area.left && 
       this.bottom==area.bottom && 
       this.top==area.top; 
    } 
} 

ОБНОВЛЕНИЕ

В API 22 (Android 5.1), Android изменилось CalendarView к MATERIAL. Там нет ListView вообще, проверьте код от CalendarView MODE_MATERIAL и calendarViewMode

Таким образом, приведенный выше код не работает, если вы используете стиль по умолчанию, единственный способ это сила, используя CalendarViewLegacyDelegate, что означает набор calendarViewMode для MODE_HOLO. Вы можете сделать это, добавив style="@android:style/Widget.CalendarView" который просто так:

<CalendarView 
    android:id="@+id/calendarforstart" 
    style="@android:style/Widget.CalendarView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

Но вы потеряли свой материальный интерфейс. Вы можете распространять Widget.CalendarView для ваших нужд, но я думаю, что он еще далек от материала.

+0

Эй! Спасибо за быстрый ответ. Извините, что задержка была в путешествии, и я мог попробовать это, а также ответить вам. Я пробую это сейчас и держу вас в курсе? Быстрый вопрос, почему вам нужен listView здесь? – TheDevMan

+0

Также месяц в апреле 2015 года, но когда я получаю тост, он показывает марш? Любая идея с этим? – TheDevMan

+0

Wow ... @ tianyu ... Ты спас мой день! Спасибо большое! Это работает .. Не могли бы вы объяснить, что именно вы сделали? – TheDevMan

Смежные вопросы