如何制作一个轮选器

10

我正在尝试制作一个像这个的轮子选择器。我尝试下载了该项目,但.zip文件中仅包含wheel-demo.apk和notes.txt两个文件。Notes.txt没有任何关于如何在android studio中使用此文件的说明。我找到了一篇文章建议使用ListViews创建相同的效果。我花了另外一天搜索互联网,找到了这里的源代码,但是当我将文件导入我的项目时,IDE显示了数十个错误。通过试错,我设法修复了除3个错误以外的所有错误。我相信下面是相关的代码:

MainActivity.java:
OnWheelScrollListener scrolledListener = new OnWheelScrollListener()
{
    public void onScrollingStarted(WheelView wheel)
    {
        wheelScrolled = true;// "Cannot resolve symbol wheelScrolled
    }

    public void onScrollingFinished(WheelView wheel)
    {
        wheelScrolled = false;// "Cannot resolve symbol wheelScrolled
        updateStatus();
    }
};

// Wheel changed listener
private final OnWheelChangedListener changedListener = new OnWheelChangedListener()
{
    public void onChanged(WheelView wheel, int oldValue, int newValue)
    {
        if (!wheelScrolled)// "Cannot resolve symbol wheelScrolled
        {
            updateStatus();
        }
    }
};
private void initWheel1(int id)
{
    WheelView wheel = (WheelView) findViewById(id);
    wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu1)); //cannot resolve method 'setAdapter(com.projectname.ArrayWheelAdapter<Java.lang.String>)
    wheel.setVisibleItems(2);
    wheel.setCurrentItem(0);
    wheel.addChangingListener(changedListener);
    wheel.addScrollingListener(scrolledListener);
}

OnWheelScrollListener.java:

public interface OnWheelScrollListener {
/**
 * Callback method to be invoked when scrolling started.
 * @param wheel the wheel view whose state has changed.
 */
void onScrollingStarted(WheelView wheel);

/**
 * Callback method to be invoked when scrolling ended.
 * @param wheel the wheel view whose state has changed.
 */
void onScrollingFinished(WheelView wheel);}

OnWheelChangedListener.java:

    public interface OnWheelChangedListener {
    /**
     * Callback method to be invoked when current item changed
     * @param wheel the wheel view whose state has changed
     * @param oldValue the old value of current item
     * @param newValue the new value of current item
     */
    void onChanged(WheelView wheel, int oldValue, int newValue);
}

ArrayWheelAdapter.java

    public class ArrayWheelAdapter<T> extends AbstractWheelTextAdapter {

    // items
    private T items[];

    /**
     * Constructor
     * @param context the current context
     * @param items the items
     */
    public ArrayWheelAdapter(Context context, T items[]) {
        super(context);

        //setEmptyItemResource(TEXT_VIEW_ITEM_RESOURCE);
        this.items = items;
    }

    @Override
    public CharSequence getItemText(int index) {
        if (index >= 0 && index < items.length) {
            T item = items[index];
            if (item instanceof CharSequence) {
                return (CharSequence) item;
            }
            return item.toString();
        }
        return null;
    }

    @Override
    public int getItemsCount() {
        return items.length;
    }
}

所有3个.java文件已经在MainActivity的导入列表中添加,认为这可能会解决问题,但并没有。感谢迄今为止的建议。

你试过那种方法了吗? - Aditya Vyas-Lakhan
你是什么意思?帖子中的代码是我在遇到我提到的滚动问题之前所做的。Android Studio无法从我从下载的.zip文件中提取的文件中导入任何内容。 - ComSof-I
您可以使用“导出到Github”按钮将整个项目从https://code.google.com/p/android-wheel/导出到GitHub。这样做可以轻松地导入Android Studio中。 - Kuffs
2个回答

67
您可以直接使用Android API 11及以上版本中包含的默认NumberPicker。您可以使用NumberPicker#setDisplayedValues显示任何想要的字符串。

XML:

<NumberPicker
        android:id="@+id/number_picker"
        android:layout_width="match_parent"
        android:layout_height="180dp" />

Java:

    String[] data = new String[]{"Berlin", "Moscow", "Tokyo", "Paris"};
    picker.setMinValue(0);
    picker.setMaxValue(data.length-1);
    picker.setDisplayedValues(data);

截图:

NumberPicker displaying Cities


谢谢你的建议,下次我会尝试这种方法。 - ComSof-I
2
API级别11中添加 - Mir-Ismaili
2
如何删除分割线 - blackHawk
1
嘿,我该如何根据项目值以特定颜色绘制每个项目? - Tigran Babajanyan
1
如果我需要同时显示超过3个项目怎么办? - Hamzeh Soboh
显示剩余2条评论

14

试一试

public class MainActivity extends Activity
    {
        // TODO: Externalize string-array
        String wheelMenu1[] = new String[]{"name 1", "name 2", "name 3", "name 4", "name 5", "name 6","name 7","name 8","name 9"};
        String wheelMenu2[] = new String[]{"age 1", "age 2", "age 3"};
        String wheelMenu3[] = new String[]{"10", "20","30","40","50","60"};

        // Wheel scrolled flag
        private boolean wheelScrolled = false;

        private TextView text;
        private EditText text1;
        private EditText text2;
        private EditText text3;

        @Override
        public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                initWheel1(R.id.p1);
                initWheel2(R.id.p2);
                initWheel3(R.id.p3);

                text1 = (EditText) this.findViewById(R.id.r1);
                text2 = (EditText) this.findViewById(R.id.r2);
                text3 = (EditText) this.findViewById(R.id.r3);
                text = (TextView) this.findViewById(R.id.result);
            }

        // Wheel scrolled listener
        OnWheelScrollListener scrolledListener = new OnWheelScrollListener()
            {
                public void onScrollStarts(WheelView wheel)
                    {
                        wheelScrolled = true;
                    }

                public void onScrollEnds(WheelView wheel)
                    {
                        wheelScrolled = false;
                        updateStatus();
                    }
            };

        // Wheel changed listener
        private final OnWheelChangedListener changedListener = new OnWheelChangedListener()
            {
                public void onChanged(WheelView wheel, int oldValue, int newValue)
                    {
                        if (!wheelScrolled)
                            {
                                updateStatus();
                            }
                    }
            };

        /**
         * Updates entered PIN status
         */
        private void updateStatus()
            {
                text1.setText(wheelMenu1[getWheel(R.id.p1).getCurrentItem()]);
                text2.setText(wheelMenu2[getWheel(R.id.p2).getCurrentItem()]);
                text3.setText(wheelMenu3[getWheel(R.id.p3).getCurrentItem()]);

                text.setText(wheelMenu1[getWheel(R.id.p1).getCurrentItem()] + " - " + wheelMenu2[getWheel(R.id.p2).getCurrentItem()] + " - " + wheelMenu3[getWheel(R.id.p3).getCurrentItem()]);
            }

        /**
         * Initializes wheel
         * 
         * @param id
         *          the wheel widget Id
         */

        private void initWheel1(int id)
            {
                WheelView wheel = (WheelView) findViewById(id);
                wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu1));
                wheel.setVisibleItems(2);
                wheel.setCurrentItem(0);
                wheel.addChangingListener(changedListener);
                wheel.addScrollingListener(scrolledListener);
            }

        private void initWheel2(int id)
            {
                WheelView wheel = (WheelView) findViewById(id);
                wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu2));
                wheel.setVisibleItems(2);
                wheel.setCurrentItem(0);
                wheel.addChangingListener(changedListener);
                wheel.addScrollingListener(scrolledListener);
            }

        private void initWheel3(int id)
            {
                WheelView wheel = (WheelView) findViewById(id);

                wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu3));
                wheel.setVisibleItems(2);
                wheel.setCurrentItem(0);
                wheel.addChangingListener(changedListener);
                wheel.addScrollingListener(scrolledListener);
            }

        /**
         * Returns wheel by Id
         * 
         * @param id
         *          the wheel Id
         * @return the wheel with passed Id
         */
        private WheelView getWheel(int id)
            {
                return (WheelView) findViewById(id);
            }

        /**
         * Tests wheel value
         * 
         * @param id
         *          the wheel Id
         * @param value
         *          the value to test
         * @return true if wheel value is equal to passed value
         */
        private int getWheelValue(int id)
            {
                return getWheel(id).getCurrentItem();
            }
    }
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/layout_bg">


    <LinearLayout
        android:layout_marginTop="24dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.example.wheel.WheelView
            android:id="@+id/p1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
        <com.example.wheel.WheelView
            android:id="@+id/p2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
        <com.example.wheel.WheelView
            android:id="@+id/p3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp">
        <EditText
            android:id="@+id/r1"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_gravity="center_horizontal"
            android:textSize="18sp"
            android:textColor="#000">
        </EditText>
        <EditText
            android:id="@+id/r2"
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_gravity="center_horizontal"
            android:textSize="18sp"
            android:textColor="#000">
        </EditText>
        <EditText
            android:id="@+id/r3"
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_gravity="center_horizontal"
            android:textSize="18sp"
            android:textColor="#000">
        </EditText>
    </LinearLayout>

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_gravity="center_horizontal"
        android:textSize="18sp"
        android:textColor="#FFF"
        android:text="Your choice">
    </TextView>
</LinearLayout>

也可以尝试这个演示

在此输入图片描述


谢谢您提供的代码,我会尝试将其整合到项目中。不过有一件事,其中一些代码似乎依赖于导入的代码。我无法让我下载的wheel-demo项目正常工作,希望这段代码不依赖于那个项目。 - ComSof-I
有很多“无法解析符号'随机符号'”,我认为这是由于未正确导入轮选择器到项目中所致。找到了这个教程http://tolkianaa.blogspot.mx/2012/03/do-not-try-to-reinvent-wheel.html。但对于将正确的文件导入到我的Android Studio项目中并没有帮助。 - ComSof-I
好的,终于将GitHub上的轮子成功地引入了我的项目中,除了顶部代码框中的一行代码之外都能正常工作。这一行代码是:wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu1)); 我已经花了几个小时查看我的项目,但我无法找到为什么这一行代码会显示IDE错误的原因。 - ComSof-I
wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu1)); 需要改为:wheel.setViewAdapter(new ArrayWheelAdapter<String>(wheelMenu1)); 现在可以正常工作了!! - ComSof-I
我该如何使这个滚轮选择器像示例中那样自动滚动?我想使用它来自动选择一些值(就像老虎机一样)。 - Raluca Lucaci

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接