如果用户使用键盘输入时间,TimePicker 仅显示分钟并设置最大分钟

发布于 2024-10-13 07:58:45 字数 2031 浏览 5 评论 0原文

这就是我的时间选择器目前的样子。 我希望它只显示分钟,并且最多只显示 30 分钟。 它工作正常,但是当你用键盘输入时间时,它会将时间设置为 0,但我希望它达到最大值。 因此,如果用户输入 50,它应该变为 30,我该怎么做? 我怎样才能隐藏时间选择器的时间,因为这里不需要它。

protected void onCreate(Bundle savedInstanceState) {
    System.out.println("Powernap");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.powernap);
    TimePicker pn = (TimePicker) findViewById(R.id.tp_powernap);

    pn.setOnTimeChangedListener(StartTimeChangedListener);
    pn.setIs24HourView(true);
}

    private TimePicker.OnTimeChangedListener StartTimeChangedListener =
        new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            updateDisplay(view, hourOfDay, minute);          
        }
    };

    private TimePicker.OnTimeChangedListener NullTimeChangedListener =
        new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

        }
    };

    private void updateDisplay(TimePicker timePicker, int hourOfDay, int minute) { 

        // do calculation of next time 
        int nextMinute = 0;
        int nextHour = 0;
        if (minute > 30){
            System.out.println("Minute+30");
            nextMinute = 30;
            // set minute
            timePicker.setCurrentMinute(nextMinute);
        }
        else {          
            System.out.println("minute 0");
            nextMinute = 0;
        }
        if (hourOfDay != 0){
            System.out.println("hour>0");
            nextHour = 24;
        }

        // remove ontimechangedlistener to prevent stackoverflow/infinite loop
        timePicker.setOnTimeChangedListener(NullTimeChangedListener);

        // set minute
        timePicker.setCurrentMinute(timePicker.getCurrentMinute()+nextMinute);

        //set hour
        timePicker.setCurrentHour(nextHour);

        // hook up ontimechangedlistener again
        timePicker.setOnTimeChangedListener(StartTimeChangedListener);

    }

This is how my timepicker looks at the moment.
I want it to only display the minutes and the minutes only to maximum 30.
It works fine but when u enter the time with the keyboard it sets the time to 0 but i want it to go to the maximum.
so if a user enters 50 it should go to 30, how can i do this?
and how can i hide the hour of the timepicker cause it's not needed here.

protected void onCreate(Bundle savedInstanceState) {
    System.out.println("Powernap");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.powernap);
    TimePicker pn = (TimePicker) findViewById(R.id.tp_powernap);

    pn.setOnTimeChangedListener(StartTimeChangedListener);
    pn.setIs24HourView(true);
}

    private TimePicker.OnTimeChangedListener StartTimeChangedListener =
        new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            updateDisplay(view, hourOfDay, minute);          
        }
    };

    private TimePicker.OnTimeChangedListener NullTimeChangedListener =
        new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

        }
    };

    private void updateDisplay(TimePicker timePicker, int hourOfDay, int minute) { 

        // do calculation of next time 
        int nextMinute = 0;
        int nextHour = 0;
        if (minute > 30){
            System.out.println("Minute+30");
            nextMinute = 30;
            // set minute
            timePicker.setCurrentMinute(nextMinute);
        }
        else {          
            System.out.println("minute 0");
            nextMinute = 0;
        }
        if (hourOfDay != 0){
            System.out.println("hour>0");
            nextHour = 24;
        }

        // remove ontimechangedlistener to prevent stackoverflow/infinite loop
        timePicker.setOnTimeChangedListener(NullTimeChangedListener);

        // set minute
        timePicker.setCurrentMinute(timePicker.getCurrentMinute()+nextMinute);

        //set hour
        timePicker.setCurrentHour(nextHour);

        // hook up ontimechangedlistener again
        timePicker.setOnTimeChangedListener(StartTimeChangedListener);

    }

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

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

发布评论

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

评论(2

但可醉心 2024-10-20 07:58:45

这就是我用最大和最小以及 5 分钟步骤完成部分的方法,stackoverflow 上有示例,但使用他们的方法你不能同时使用向上和向下按钮,用我的方法它可以工作:

public class Powernap extends Activity {



/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    System.out.println("Powernap");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.powernap);
    TimePicker pn = (TimePicker) findViewById(R.id.tp_powernap);
    pn.setCurrentMinute(5);
    System.out.println("timepicker erstellt");
    pn.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
    System.out.println("Tastatur geblockt");
    pn.setOnTimeChangedListener(StartTimeChangedListener);
    System.out.println("starttimechangedlistener");
    pn.setIs24HourView(true);
}


    private TimePicker.OnTimeChangedListener StartTimeChangedListener =
        new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            updateDisplay(view, hourOfDay, minute);          
        }
    };

    private TimePicker.OnTimeChangedListener NullTimeChangedListener =
        new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

        }
    };

    private void updateDisplay(TimePicker timePicker, int hourOfDay, int minute) { 

        // do calculation of next time 
        int nextMinute = 0;
        int nextHour = 0;
        if (minute >= 30 && minute <= 59){
            System.out.println("Minute+30");
            nextMinute = 30;
        }
        else if(minute == 26){
            System.out.println("Minute auf 30");
            nextMinute = 30;
        }
        else if(minute == 25 || minute == 21 || minute == 29){
            System.out.println("Minute auf 25");
            nextMinute = 25;
        }
        else if(minute == 20 || minute == 16 || minute == 24){
            System.out.println("Minute auf 20");
            nextMinute = 20;
        }
        else if(minute == 15 || minute == 11 || minute == 19){
            System.out.println("Minute auf 15");
            nextMinute = 15;
        }
        else if(minute == 10 || minute == 6 || minute == 14){
            System.out.println("Minute auf 10");
            nextMinute = 10;
        }
        else if(minute == 5 || minute == 1 || minute == 9 || minute < 5){
            System.out.println("Minute auf 5");
            nextMinute = 5;
        }





        if (hourOfDay != 0){
            System.out.println("hour>0");
            nextHour = 24;
        }

        // remove ontimechangedlistener to prevent stackoverflow/infinite loop
        timePicker.setOnTimeChangedListener(NullTimeChangedListener);

        // set minute
        timePicker.setCurrentMinute(nextMinute);

        //set hour
        timePicker.setCurrentHour(nextHour);

        // hook up ontimechangedlistener again
        timePicker.setOnTimeChangedListener(StartTimeChangedListener);

    }

that's how i did the part with the maximum and minimum and the 5mins steps, there are samples on stackoverflow but with their methods u can't use both the up and the down button, with my method it works:

public class Powernap extends Activity {



/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    System.out.println("Powernap");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.powernap);
    TimePicker pn = (TimePicker) findViewById(R.id.tp_powernap);
    pn.setCurrentMinute(5);
    System.out.println("timepicker erstellt");
    pn.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
    System.out.println("Tastatur geblockt");
    pn.setOnTimeChangedListener(StartTimeChangedListener);
    System.out.println("starttimechangedlistener");
    pn.setIs24HourView(true);
}


    private TimePicker.OnTimeChangedListener StartTimeChangedListener =
        new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            updateDisplay(view, hourOfDay, minute);          
        }
    };

    private TimePicker.OnTimeChangedListener NullTimeChangedListener =
        new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

        }
    };

    private void updateDisplay(TimePicker timePicker, int hourOfDay, int minute) { 

        // do calculation of next time 
        int nextMinute = 0;
        int nextHour = 0;
        if (minute >= 30 && minute <= 59){
            System.out.println("Minute+30");
            nextMinute = 30;
        }
        else if(minute == 26){
            System.out.println("Minute auf 30");
            nextMinute = 30;
        }
        else if(minute == 25 || minute == 21 || minute == 29){
            System.out.println("Minute auf 25");
            nextMinute = 25;
        }
        else if(minute == 20 || minute == 16 || minute == 24){
            System.out.println("Minute auf 20");
            nextMinute = 20;
        }
        else if(minute == 15 || minute == 11 || minute == 19){
            System.out.println("Minute auf 15");
            nextMinute = 15;
        }
        else if(minute == 10 || minute == 6 || minute == 14){
            System.out.println("Minute auf 10");
            nextMinute = 10;
        }
        else if(minute == 5 || minute == 1 || minute == 9 || minute < 5){
            System.out.println("Minute auf 5");
            nextMinute = 5;
        }





        if (hourOfDay != 0){
            System.out.println("hour>0");
            nextHour = 24;
        }

        // remove ontimechangedlistener to prevent stackoverflow/infinite loop
        timePicker.setOnTimeChangedListener(NullTimeChangedListener);

        // set minute
        timePicker.setCurrentMinute(nextMinute);

        //set hour
        timePicker.setCurrentHour(nextHour);

        // hook up ontimechangedlistener again
        timePicker.setOnTimeChangedListener(StartTimeChangedListener);

    }
喜爱皱眉﹌ 2024-10-20 07:58:45

您将需要编写自己的小部件。 TimePicker 小部件或 TimePickerDialog 不支持您想要的功能。

You will need to write your own widget. What you want is not supported by the TimePicker widget or TimePickerDialog.

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