hour/minute picker for android countdown timer

雨燕双飞 提交于 2019-12-03 02:47:42
Arnoud

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;
    }
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!