Android 中的 UI 选择器

Posted

技术标签:

【中文标题】Android 中的 UI 选择器【英文标题】:UI Picker in Android 【发布时间】:2016-07-19 08:57:38 【问题描述】:

我想构建一个这样的自定义项目选择器

我想听听有关如何使用时间选择器构建这个东西的任何想法,但我想这不是正确的方法。

【问题讨论】:

查看此代码可能会有所帮助:code.google.com/archive/p/android-wheel/downloads 尝试至少在谷歌上搜索 ***.com/questions/24833827/… 【参考方案1】:

鲍里斯,

你应该使用android材质选择器,这里是android库 - https://github.com/wdullaer/MaterialDateTimePicker

如果你想制作 ios 类型选择器视图,你可以使用这个库 - https://github.com/AigeStudio/WheelPicker

【讨论】:

【参考方案2】:

这叫wheel picker

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

也可以查看demo

【讨论】:

以上是关于Android 中的 UI 选择器的主要内容,如果未能解决你的问题,请参考以下文章

更改 Android TV 应用程序的日期选择器 UI

无法在 detox 中的 android 日期选择器中选择 month_view 中的日期

如何在 xamarin android ui 测试自动化中使用日期选择器

同一个视图控制器Objective C中的两个UI选择器?

通过单击 Android Map Fragment 中的地图来隐藏 UI 元素

Charles - Android 多媒体选择器