簡體   English   中英

時間選擇器與Android滾動?

[英]time picker with scroll for android?

我希望在主視圖中有兩個用於選擇時間的滾輪,一個滾動輪包含01-24的數字,另一個滾動輪包含00-60的數字,跳數為5(0,5,10..55),是否有准備好進行這樣的滾動事情? 類似於http://code.google.com/p/mobiscroll/ ,只是android內置。 時間選擇器很煩人。我需要按+-直到我選擇了我想要的東西。 我更喜歡觸摸直觀的東西。 對於這種事情,我有哪些選擇?

沒有內置內容,但是您可以嘗試以下方法: http : //code.google.com/p/android-wheel/

在這里,您將找到帶有源代碼的完整項目。 TestWheel.zip

  1. 從鏈接下載TestWheel.zip
  2. 將其導入您的Eclipse
  3. 將此項目轉換為庫項目(右鍵單擊testWheel項目==>屬性==> android ==>選中is library復選框)。
  4. 將此庫項目添加到您的項目中。
  5. 將以下代碼復制並粘貼到XML文件中。

在此處輸入圖片說明

這是您的hour_listView。 只需為其設置一個字符串適配器。 假設您可以使用ArrayAdapter填充listView。

        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");
                    }
                }
            }

然后設置在button.clickListener上,您將獲得可見listView的中心行號。

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

關鍵點很少。 *設置listView 1dp的分隔線高度。 *使用線性布局在listView中插入行高應為listview總高度的1/3(以dp為單位)。

列表視圖和按鈕的XML

<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。

<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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM