是否可以制作水平NumberPicker?

时间:2011-07-22 21:17:06

标签: android user-interface

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

是否可以将其设为水平?使用数字左侧和右侧的按钮?

6 个答案:

答案 0 :(得分:11)

你不能改变NumberPicker AFAIK,但写你自己应该相当简单。

答案 1 :(得分:3)

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

这是它的外观:

horizontal number picker screenshot

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

布局文件 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();

答案 2 :(得分:2)

不。实际上,即使它出现在“图形布局”构建器中,您也无法真正使用它。它仍然受到保护。

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

答案 3 :(得分:1)

我找不到一个可靠的解决方案,所以我最终制作了自己的解决方案,但我需要它看起来像一个圆形的齿轮,因此它适用于该用例。

你可以从源头获得这个想法..这就是:

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

答案 4 :(得分:0)

机器人:旋转=&#34; 90&#34;   到90度或者你喜欢的值可以是负数它会旋转视图但是.next需要自定义文本显示水平

答案 5 :(得分:0)

您可以从默认的垂直 NumberPicker 中创建一个水平 NumberPicker,就像您可以从默认的水平 SeekBar 中创建一个 垂直 SeekBar

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

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

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

enter image description here

源代码:

Custom NumberPicker classExtended Horizontal NumberPicker

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