Android Open Source - android-wheel Time Activity






From Project

Back to project page android-wheel.

License

The source code is released under:

Apache License

If you think the Android project android-wheel listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package kankan.wheel.demo;
/*  w w w.j a  v a  2 s . c om*/
import java.util.Calendar;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelClickedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TimePicker;

public class TimeActivity extends Activity {
  // Time changed flag
  private boolean timeChanged = false;

  // Time scrolled flag
  private boolean timeScrolled = false;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.time_layout);

    final WheelView hours = (WheelView) findViewById(R.id.hour);
    hours.setViewAdapter(new NumericWheelAdapter(this, 0, 23));

    final WheelView mins = (WheelView) findViewById(R.id.mins);
    mins.setViewAdapter(new NumericWheelAdapter(this, 0, 59, "%02d"));
    mins.setCyclic(true);

    final TimePicker picker = (TimePicker) findViewById(R.id.time);
    picker.setIs24HourView(true);

    // set current time
    Calendar c = Calendar.getInstance();
    int curHours = c.get(Calendar.HOUR_OF_DAY);
    int curMinutes = c.get(Calendar.MINUTE);

    hours.setCurrentItem(curHours);
    mins.setCurrentItem(curMinutes);

    picker.setCurrentHour(curHours);
    picker.setCurrentMinute(curMinutes);

    // add listeners
    addChangingListener(mins, "min");
    addChangingListener(hours, "hour");

    OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
      public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (!timeScrolled) {
          timeChanged = true;
          picker.setCurrentHour(hours.getCurrentItem());
          picker.setCurrentMinute(mins.getCurrentItem());
          timeChanged = false;
        }
      }
    };
    hours.addChangingListener(wheelListener);
    mins.addChangingListener(wheelListener);

    OnWheelClickedListener click = new OnWheelClickedListener() {
            public void onItemClicked(WheelView wheel, int itemIndex) {
                wheel.setCurrentItem(itemIndex, true);
            }
        };
        hours.addClickingListener(click);
        mins.addClickingListener(click);

    OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
      public void onScrollingStarted(WheelView wheel) {
        timeScrolled = true;
      }
      public void onScrollingFinished(WheelView wheel) {
        timeScrolled = false;
        timeChanged = true;
        picker.setCurrentHour(hours.getCurrentItem());
        picker.setCurrentMinute(mins.getCurrentItem());
        timeChanged = false;
      }
    };

    hours.addScrollingListener(scrollListener);
    mins.addScrollingListener(scrollListener);

    picker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
      public void onTimeChanged(TimePicker  view, int hourOfDay, int minute) {
        if (!timeChanged) {
          hours.setCurrentItem(hourOfDay, true);
          mins.setCurrentItem(minute, true);
        }
      }
    });
  }

  /**
   * Adds changing listener for wheel that updates the wheel label
   * @param wheel the wheel
   * @param label the wheel label
   */
  private void addChangingListener(final WheelView wheel, final String label) {
    wheel.addChangingListener(new OnWheelChangedListener() {
      public void onChanged(WheelView wheel, int oldValue, int newValue) {
        //wheel.setLabel(newValue != 1 ? label + "s" : label);
      }
    });
  }
}




Java Source Code List

kankan.wheel.demo.CitiesActivity.java
kankan.wheel.demo.DateActivity.java
kankan.wheel.demo.PasswActivity.java
kankan.wheel.demo.SlotMachineActivity.java
kankan.wheel.demo.SpeedActivity.java
kankan.wheel.demo.Time2Activity.java
kankan.wheel.demo.TimeActivity.java
kankan.wheel.demo.WheelDemo.java
kankan.wheel.widget.ItemsRange.java
kankan.wheel.widget.OnWheelChangedListener.java
kankan.wheel.widget.OnWheelClickedListener.java
kankan.wheel.widget.OnWheelScrollListener.java
kankan.wheel.widget.WheelAdapter.java
kankan.wheel.widget.WheelRecycle.java
kankan.wheel.widget.WheelScroller.java
kankan.wheel.widget.WheelView.java
kankan.wheel.widget.adapters.AbstractWheelAdapter.java
kankan.wheel.widget.adapters.AbstractWheelTextAdapter.java
kankan.wheel.widget.adapters.AdapterWheel.java
kankan.wheel.widget.adapters.ArrayWheelAdapter.java
kankan.wheel.widget.adapters.NumericWheelAdapter.java
kankan.wheel.widget.adapters.WheelViewAdapter.java