简体   繁体   中英

time picker with scroll for android?

I wish to have in my main view two scroll wheels for choosing time, one contains digits from 01-24, the other 00-60, with jumps of 5 (0,5,10..55) Is there something ready for such a thing? similar to http://code.google.com/p/mobiscroll/ , just android built-in. The time picker is quite annoying..i need to press +- till i pick what i wish. i prefer something more touch-intuitive. What options do i have exists for such a thing?

没有内置内容,但是您可以尝试以下方法: http : //code.google.com/p/android-wheel/

Here you will find a complete project with source code. TestWheel.zip

  1. download the TestWheel.zip from the link.
  2. Import it in your Eclipse
  3. Convert this project into a library Project (rightclick on th testWheel project ==> properties ==> android ==> chek the is library check box).
  4. Add this library project to your project.
  5. copy and paste the following code in your XML file.

在此处输入图片说明

Here's your hour_listView. Simply set a string adapter for it. Assuming that you can populate a listView using ArrayAdapter.

        hr_list.setOnScrollListener(new OnScrollListener() {

            int currentcount,topitem,firstitem,lastitem;
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE && currentcount == 4) {
                    while (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                    }

                    LinearLayout item = (LinearLayout) view.getChildAt(0);
                    int height = item.getHeight();
                    int top = Math.abs(item.getTop()); // top is a negative value

                    if (top <= height/2){
                        view.smoothScrollToPositionFromTop(topitem, 0, 3000);
                        Log.d("Switch", "up");
                    }
                    if(top> height/2){
                        view.smoothScrollToPositionFromTop(topitem+1, 0,3000);
                        Log.d("Switch", "down");
                    }
                }
            }

And then set on a button.clickListener and you'll get the center row-number of the visible listView.

int list_hr = hr_list.getFirstVisiblePosition() - hr_list.getHeaderViewsCount();

Few key point. * set divider height for listView 1dp. *Use Linear layout to infalte in a listView The row height should be 1/3 of the total listview heigth (in dp).

XML for listview and button

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Snap List"
    android:textSize="25sp" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="100dp"
    android:layout_height="120dp"
    android:fadingEdgeLength="33dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="73dp"
    android:background="@drawable/lisbg"
    android:scrollbars="none" >
</ListView>

<StackView
    android:id="@+id/stackView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/listView1"
    android:layout_marginLeft="103dp"
    android:layout_marginTop="57dp"
    android:layout_toRightOf="@+id/textView1" >
</StackView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="58dp"
    android:text="OK" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:text="Scroll 5" />

XML for the row.

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:textSize="25sp"
    android:gravity="center_vertical|center_horizontal"
    android:layout_gravity="center_vertical|center_horizontal"
    android:text="TextView" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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