Android 倒计时器的小时/分钟选择器

发布于 2024-09-24 09:13:16 字数 223 浏览 5 评论 0原文

我正在尝试实现类似倒计时器的东西,在 0 时播放警报。我希望能够设置计时器响起之前等待的时间,我想知道是否有一个 UI 小部件或元素可以提供这种选择功能。

基本上,android有类似iPhone选择旋转轮的东西吗?或者是否有某种类型的时间选择器允许选择任意数量的小时和分钟? Android 中的时间选择器小部件有一个不必要的 AM/PM 标签。

我是否需要实现自己的自定义 UI 才能实现此目的?

I'm trying to implement something like a countdown timer that plays an alarm at 0. I want to be able to set the amount of time to wait before the timer goes off and I'm wondering if there's a UI widget or element that provides this kind of selection functionality.

Basically, does android have something like the iPhone's selection spinwheel? Or is there some type of timepicker that allows selection of an arbitrary number of hours and minutes? The timepicker widget in android has an unnecessary AM/PM label.

Do I need to implement my own custom UI to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经大条 2024-10-01 09:13:16

您可以使用默认的 TimePickerDialog 并重写 onTimeChanged 方法来自行更新标题:

public class DurationPickerDialog extends TimePickerDialog {

    public DurationPickerDialog(Context context, int theme,
        OnTimeSetListener callBack, int hour, int minute) {
        super(context, theme, callBack, hour, minute, true);
        updateTitle(hour, minute);
    }

    public DurationPickerDialog(Context context, OnTimeSetListener callBack,
        int hour, int minute) {
        super(context, callBack, hour, minute, true);
        updateTitle(hour, minute);
    }

    @Override
    public void onTimeChanged(TimePicker view, int hour, int minute) {
        super.onTimeChanged(view, hour, minute);
        updateTitle(hour, minute);
    }

    public void updateTitle(int hour, int minute) {
        setTitle("Duration: " + hour + ":" + formatNumber(minute));
    }

    private String formatNumber(int number) {
        String result = "";
        if (number < 10) {
            result += "0";
        }
        result += number;

        return result;
    }
}

You can use the default TimePickerDialog and override the onTimeChanged method to update the title yourself:

public class DurationPickerDialog extends TimePickerDialog {

    public DurationPickerDialog(Context context, int theme,
        OnTimeSetListener callBack, int hour, int minute) {
        super(context, theme, callBack, hour, minute, true);
        updateTitle(hour, minute);
    }

    public DurationPickerDialog(Context context, OnTimeSetListener callBack,
        int hour, int minute) {
        super(context, callBack, hour, minute, true);
        updateTitle(hour, minute);
    }

    @Override
    public void onTimeChanged(TimePicker view, int hour, int minute) {
        super.onTimeChanged(view, hour, minute);
        updateTitle(hour, minute);
    }

    public void updateTitle(int hour, int minute) {
        setTitle("Duration: " + hour + ":" + formatNumber(minute));
    }

    private String formatNumber(int number) {
        String result = "";
        if (number < 10) {
            result += "0";
        }
        result += number;

        return result;
    }
}
零度℉ 2024-10-01 09:13:16

是的,你可以一起破解一个。尽管由于 API 的变化,这段代码的生命周期很可能会很短。

int foo = 7;
Object o = findViewById(ids[i]);
Class<? extends Object> c = o.getClass();
Method m = c.getMethod("setCurrent", int.class);
m.invoke(o, foo);

String[] displayedValues = new String[] { "$00", "$01", "$02", "$03", "$04", 
                        "$05", "$06", "$07", "$08", "$09", "$10", "$11", 
                        "$12", "$13", "$14", "$15", "$16", "$17", "$18", "$19", "$20", "$21", 
                        "$22", "$23", "$24", "$25"};
Method m = c.getMethod("setRange", int.class, int.class, String[].class);
M.invoke(o, 0, 25, displayedValues);

然后在布局中放置其中几个:

<com.android.internal.widget.NumberPicker
android:id="@+id/picker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"  />

setCurrent 设置选择器的索引,在 foo 的情况下意味着值“$07”。 setRange 设置要在选择器中显示的值。

祝你好运。

Yes, you can hack one together. It's a distinct possibility that this code will have a short lifespan though due to changes in the API.

int foo = 7;
Object o = findViewById(ids[i]);
Class<? extends Object> c = o.getClass();
Method m = c.getMethod("setCurrent", int.class);
m.invoke(o, foo);

String[] displayedValues = new String[] { "$00", "$01", "$02", "$03", "$04", 
                        "$05", "$06", "$07", "$08", "$09", "$10", "$11", 
                        "$12", "$13", "$14", "$15", "$16", "$17", "$18", "$19", "$20", "$21", 
                        "$22", "$23", "$24", "$25"};
Method m = c.getMethod("setRange", int.class, int.class, String[].class);
M.invoke(o, 0, 25, displayedValues);

And then drop a couple of these in your layout:

<com.android.internal.widget.NumberPicker
android:id="@+id/picker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"  />

setCurrent set's the index of the picker which in the case of foo means a value of "$07". setRange sets the values to display in the picker.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文