安卓:设定时间,时间选择器的时间显示在文本视图 [英] Android: Setting time in time picker with the time shown in text view

查看:411
本文介绍了安卓:设定时间,时间选择器的时间显示在文本视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我显示的时间在文本视图为19:00。在点击的文本视图,一时间选择器对话框弹出,。在这段时间选择器,我必须准确显示同一时间是什么出现在TextView中。但是,我没有得到如何做到这一点。

code

 的SimpleDateFormat自卫队=新的SimpleDateFormat(HH:mm的);
        // INT HR = 0;
        日期日期= NULL;
        尝试
        {
            日期= sdf.parse(resDateArray [3]);
        }
        赶上(ParseException的E){
            e.printStackTrace();
        }
        最后台历挂历= Calendar.getInstance();
        calendar.setTime(日期);
        // TP是引用变量的时间选择器

        tp.setCurrentHour(calendar.get(Calendar.HOUR));
        tp.setCurrentMinute(calendar.get(Calendar.MINUTE));

        }//其他
 

解决方案

您应该使用的SimpleDateFormat 获得日期 String对象。 然后,只需拨打

  picker.setCurrentHour(date.getHours())
 

  picker.setCurrentMinute(date.getMinutes())
 

由于日期对象德precated,你应该使用日历代替它。 您可以实例化一个日历这种方式:

 日历C = Calendar.getInstance();
c.setTime(日期);
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendat.MINUTE));
 

编辑:完整的code

 的SimpleDateFormat自卫队=新的SimpleDateFormat(HH:SS);
        日期日期= NULL;
        尝试 {
            日期= sdf.parse(07);
        }赶上(ParseException的E){
        }
        日历C = Calendar.getInstance();
        c.setTime(日期);

        TimePicker选择器=新TimePicker(getApplicationContext());
        picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
        picker.setCurrentMinute(c.get(Calendar.MINUTE));
 

In my app, I am showing time in text view as 07:00 PM. On click of text view, a time picker dialog pops up,. In that time picker, I have to show exactly same time as what is appearing in textview. But, I am not getting how to do that.

CODE

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm a");
        //int hr = 0;
        Date date = null;
        try 
        {
            date = sdf.parse(resDateArray[3]);
        } 
        catch (ParseException e) {
            e.printStackTrace();
        }
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //tp is reference variable for time picker

        tp.setCurrentHour(calendar.get(Calendar.HOUR));
        tp.setCurrentMinute(calendar.get(Calendar.MINUTE));

        }//else

解决方案

You should use a SimpleDateFormat to get a Date object from your String. Then just call

picker.setCurrentHour(date.getHours())

and

picker.setCurrentMinute(date.getMinutes())

Since the Date object is deprecated, you should use a Calendar instead of it. You can instantiate a Calendar that way :

Calendar c = Calendar.getInstance();
c.setTime(date);
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendat.MINUTE));

Edit : the complete code

        SimpleDateFormat sdf = new SimpleDateFormat("hh:ss");
        Date date = null;
        try {
            date = sdf.parse("07:00");
        } catch (ParseException e) {
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        TimePicker picker = new TimePicker(getApplicationContext());
        picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
        picker.setCurrentMinute(c.get(Calendar.MINUTE));

这篇关于安卓:设定时间,时间选择器的时间显示在文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆