用于轮式选择器的 Android 阵列

标签 android arrays string

大家好,我有一个轮子选择器在工作,但目前它正在为所有轮子提取 0-9 的数字。我希望能够设置值而不是 0-9 我希望它是从数组或字符串中提取的单词,所以我可以输入它们 myslef 因为我不确定目前从哪里提取数字。代码如下:

public class PasswActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.passw_layout);
    initWheel(R.id.passw_1);
    initWheel(R.id.passw_2);
    initWheel(R.id.passw_3);
    initWheel(R.id.passw_4);

    Button mix = (Button)findViewById(R.id.btn_mix);
    mix.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mixWheel(R.id.passw_1);
            mixWheel(R.id.passw_2);
            mixWheel(R.id.passw_3);
            mixWheel(R.id.passw_4);
        }
    });


}

// Wheel scrolled flag
private boolean wheelScrolled = false;

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

    }
};

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

        }
    }
};


/**
 * Initializes wheel
 * @param id the wheel widget Id
 */
private void initWheel(int id) {
    WheelView wheel = getWheel(id);
    wheel.setViewAdapter(new NumericWheelAdapter(this, 0, 9));
    wheel.setCurrentItem((int)(Math.random() * 10));

    wheel.addChangingListener(changedListener);
    wheel.addScrollingListener(scrolledListener);
    wheel.setCyclic(true);
    wheel.setInterpolator(new AnticipateOvershootInterpolator());
}

/**
 * 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 entered PIN
 * @param v1
 * @param v2
 * @param v3
 * @param v4
 * @return true 
 */
private boolean testPin(int v1, int v2, int v3, int v4) {
    return testWheelValue(R.id.passw_1, v1) && testWheelValue(R.id.passw_2, v2) &&
        testWheelValue(R.id.passw_3, v3) && testWheelValue(R.id.passw_4, v4);
}

/**
 * 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 boolean testWheelValue(int id, int value) {
    return getWheel(id).getCurrentItem() == value;
}

/**
 * Mixes wheel
 * @param id the wheel id
 */
private void mixWheel(int id) {
    WheelView wheel = getWheel(id);
    wheel.scroll(-25 + (int)(Math.random() * 50), 2000);
}
}

任何帮助都适用。谢谢

最佳答案

能否请您引用下面的 wheel 演示项目,它对解决 wheels 中字符串数据的问题很有帮助。下面的代码也来自同一项目。

http://code.google.com/p/android-wheel/

public class CitiesActivity extends Activity {
    // Scrolling flag
    private boolean scrolling = false;

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

        setContentView(R.layout.cities_layout);

        final WheelView country = (WheelView) findViewById(R.id.country);
        country.setVisibleItems(3);
        country.setViewAdapter(new CountryAdapter(this));

        final String cities[][] = new String[][] {
                        new String[] {"New York", "Washington", "Chicago", "Atlanta", "Orlando"},
                        new String[] {"Ottawa", "Vancouver", "Toronto", "Windsor", "Montreal"},
                        new String[] {"Kiev", "Dnipro", "Lviv", "Kharkiv"},
                        new String[] {"Paris", "Bordeaux"},
                        };

        final WheelView city = (WheelView) findViewById(R.id.city);
        city.setVisibleItems(5);

        country.addChangingListener(new OnWheelChangedListener() {
                        public void onChanged(WheelView wheel, int oldValue, int newValue) {
                            if (!scrolling) {
                                updateCities(city, cities, newValue);
                            }
                        }
                });

        country.addScrollingListener( new OnWheelScrollListener() {
            public void onScrollingStarted(WheelView wheel) {
                scrolling = true;
            }
            public void onScrollingFinished(WheelView wheel) {
                scrolling = false;
                updateCities(city, cities, country.getCurrentItem());
            }
        });

        country.setCurrentItem(1);
    }

    /**
     * Updates the city wheel
     */
    private void updateCities(WheelView city, String cities[][], int index) {
        ArrayWheelAdapter<String> adapter =
            new ArrayWheelAdapter<String>(this, cities[index]);
        adapter.setTextSize(18);
        city.setViewAdapter(adapter);
        city.setCurrentItem(cities[index].length / 2);        
    }

    /**
     * Adapter for countries
     */
    private class CountryAdapter extends AbstractWheelTextAdapter {
        // Countries names
        private String countries[] =
            new String[] {"USA", "Canada", "Ukraine", "France"};
        // Countries flags
        private int flags[] =
            new int[] {R.drawable.usa, R.drawable.canada, R.drawable.ukraine, R.drawable.france};

        /**
         * Constructor
         */
        protected CountryAdapter(Context context) {
            super(context, R.layout.country_layout, NO_RESOURCE);

            setItemTextResource(R.id.country_name);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            ImageView img = (ImageView) view.findViewById(R.id.flag);
            img.setImageResource(flags[index]);
            return view;
        }

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

        @Override
        protected CharSequence getItemText(int index) {
            return countries[index];
        }
    }
}

关于用于轮式选择器的 Android 阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8170374/

相关文章:

java - 如何将图像发送到 GCP Vision API

c# - 在 C# 的字符串中间重复一个字符 n 次的最短方法是什么

android - 向 Google Play 提交多个 apk 应用

android - 如何显示修剪后的 ImageView,使高度等于宽度?

android - 将参数传递给 BroadcastReceiver

php - 从 PHP 数组中删除选定单词的有效方法

java - 字符串[][]输出问题

c - C中数组内的元素数

c++ - 查找字符串 : if found mark that string ok 中的任意子字符串

python - 在 Python 中处理字符串中的转义序列