本文介绍了如何制作拾轮器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作类似这样的车轮选择器,我尝试下载了此项目,但.zip下载中包含的唯一文件是wheel-demo.apk和notes.txt. Notes.txt没有有关如何在android studio中使用此文件的任何说明.我发现了一篇建议使用ListViews创建相同效果的帖子.我没有自己动手,而是花了一天的时间在互联网上搜索,发现了源代码这里,但是当我将文件导入到项目中时,IDE会显示许多错误.通过反复试验,我设法修复了除3个错误以外的所有错误.相当确定我已经将相关代码放在了下面
MainActivity.java:

I am trying to make a wheel picker like this I have tried downloading this project but the only files included in the .zip download are wheel-demo.apk and notes.txt. Notes.txt does not have any instructions on how to use this file with android studio. I found a post that suggested using ListViews to create the same effect. Rather than making my own I spent another day searching the internet and I found source code here but when I imported the files into my project the IDE showed dozens of errors. Through trial and error I managed to fix all but 3 errors. Pretty sure I have put the relevant code below
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:

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:

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

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的导入列表中,以为它可以解决问题,但没有解决.到目前为止,谢谢您的建议.

All 3 of the .java files have been added to the import list in MainActivity thinking it might solve the issue, it didn't.Thank you for the advice so far.

推荐答案

尝试一下

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

main.xml

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

也请尝试演示

这篇关于如何制作拾轮器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:11