【问题标题】:Is it possible to make a horizontal NumberPicker?是否可以制作水平 NumberPicker?
【发布时间】:2011-07-22 21:17:06
【问题描述】:

Android 的 NumberPicker 小部件是垂直的,在数字显示的上方和下方都有增加和减少按钮。

是否可以使其水平?用数字左右两边的按钮?

【问题讨论】:

    标签: android user-interface


    【解决方案1】:

    您无法更改 NumberPickerAFAIK,但编写自己的应该相当简单。

    【讨论】:

    • 其实可以。在 XML 中添加android:orientation="horizontal",它是水平的。不过风格不一样。
    【解决方案2】:

    我一直在寻找类似的解决方案,并根据this question 中的屏幕截图创建了一个实现。 请注意,这是相当基本的,只允许整数和设置最小值和最大值。 (您需要自己添加的任何额外功能。)

    这就是它的样子:

    您需要自定义组件的布局文件类文件,然后您可以在其他布局(和类)文件中使用它。

    布局文件numberpicker_horizo​​ntal.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <Button
            android:id="@+id/btn_less"
            android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="-" />
    
        <EditText
            android:id="@+id/et_number"
            android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textAlignment="center"
            android:inputType="number"
            android:text="0" />
    
        <Button
            android:id="@+id/btn_more"
            android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="+" />
    </LinearLayout>
    

    类文件Horizo​​ntalNumberPicker.java

    public class HorizontalNumberPicker extends LinearLayout {
        private EditText et_number;
        private int min, max;
    
        public HorizontalNumberPicker(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
    
            inflate(context, R.layout.numberpicker_horizontal, this);
    
            et_number = findViewById(R.id.et_number);
    
            final Button btn_less = findViewById(R.id.btn_less);
            btn_less.setOnClickListener(new AddHandler(-1));
    
            final Button btn_more = findViewById(R.id.btn_more);
            btn_more.setOnClickListener(new AddHandler(1));
        }
    
        /***
         * HANDLERS
         **/
    
        private class AddHandler implements OnClickListener {
            final int diff;
    
            public AddHandler(int diff) {
                this.diff = diff;
            }
    
            @Override
            public void onClick(View v) {
                int newValue = getValue() + diff;
                if (newValue < min) {
                    newValue = min;
                } else if (newValue > max) {
                    newValue = max;
                }
                et_number.setText(String.valueOf(newValue));
            }
        }
    
        /***
         * GETTERS & SETTERS
         */
    
        public int getValue() {
            if (et_number != null) {
                try {
                    final String value = et_number.getText().toString();
                    return Integer.parseInt(value);
                } catch (NumberFormatException ex) {
                    Log.e("HorizontalNumberPicker", ex.toString());
                }
            }
            return 0;
        }
    
        public void setValue(final int value) {
            if (et_number != null) {
                et_number.setText(String.valueOf(value));
            }
        }
    
        public int getMin() {
            return min;
        }
    
        public void setMin(int min) {
            this.min = min;
        }
    
        public int getMax() {
            return max;
        }
    
        public void setMax(int max) {
            this.max = max;
        }
    }
    

    然后通过以下方式将其包含在您的其他布局文件中(将 com.yourpackage 替换为您自己的包名称):

    <com.yourpackage.HorizontalNumberPicker
        android:id="@+id/np_channel_nr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    然后您可以像使用任何其他组件一样使用该组件,并使用 getValue() 方法来检索当前设置的值:

    // get a reference to the component
    final HorizontalNumberPicker np_channel_nr = dialogView.findViewById(R.id.np_channel_nr);
    
    // use value in your code
    final int nr = np_channel_nr.getValue();
    

    【讨论】:

      【解决方案3】:

      不。事实上,您甚至还不能真正使用它,即使它出现在图形布局构建器中。它仍然受到保护。

      这个问题有一些相关信息: Android Number Picker Dialog

      【讨论】:

        【解决方案4】:

        我找不到可靠的解决方案,所以最后我自己做了,但我需要它看起来像一个圆形齿轮,所以它适合那个用例。

        你可以从源头得到这个想法。它是:

        https://github.com/milosmns/actual-number-picker

        【讨论】:

          【解决方案5】:

          您可以从默认的垂直 NumberPicker 中制作一个水平 NumberPicker,就像您可以从默认的水平 SeekBar 中制作一个 vertical SeekBar

          要将垂直部件转换为水平部件,或者反过来,您需要考虑布局方法、绘图方法和触摸控制方法。但是,并非所有方法都是可覆盖的,其中一些方法是 private

          要完全控制小部件,您需要根据默认小部件编写自己的小部件,将private 方法/字段protectedextend 设为自定义小部件以进行修改。

          这是我两年前制作的即用型水平 NumberPicker:

          源代码:

          Custom NumberPicker classExtended Horizontal NumberPicker

          Another tall screenshot 显示了 Horizo​​ntalNumberPicker 和 VerticalSeekBar 的用法。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-10-23
            • 1970-01-01
            • 2021-04-10
            • 2018-06-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多