在android中使用wheel设置时间

时间:2013-03-12 10:11:51

标签: android

我使用安卓车轮作为时间选择器,并获得如下的值

private void SetTime(Context activity, int hour, int minute) {                  
    LayoutInflater inf = LayoutInflater.from(activity);
    // layout of alert box contains
    View view = inf.inflate(R.layout.wheel, null);

    final WheelView hours = (WheelView) view.findViewById(R.id.hours);
    hours.setViewAdapter(new NumericWheelAdapter(activity, 0, 23,"%02d"));          
    hours.setCyclic(true);
    hours.setCurrentItem(expiryHour);
    Log.d(TAG, "for expiryHour123456 ---> " + expiryHour);

    final WheelView mins = (WheelView) view.findViewById(R.id.minutes);
    mins.setViewAdapter(new Mywheel(activity, 0, 59,"%02d"));
    mins.setCyclic(true);           
    mins.setCurrentItem(expiryMinute);
    Log.d(TAG, "for expiryMinute123456 ---> " + expiryMinute);

    new AlertDialog.Builder(activity)
    .setMessage("Set Time For Post To AutoDelete")
    .setView(view)              
    .setPositiveButton("Set",
            new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            expiryHour = hours.getCurrentItem();
            Log.d(TAG, "for expiryHour ---> " + expiryHour);
            if(mins.getCurrentItem() > 0){
                expiryMinute = mins.getCurrentItem() + 4;
                Log.d(TAG, "for expiryMinute ---> " + expiryMinute);
            }else{
                expiryMinute = mins.getCurrentItem();
            }
        }
    });
}

当iam设置时间我得到正确的值但是,一旦设置时间,如果我想要更改时间,则分钟值显示错误。例如,首先我把它设置为2:10然后我想改变现在当我打开它的显示2:14这不应该发生。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

请查看代码中的以下行。

if(mins.getCurrentItem() > 0){
         expiryMinute = mins.getCurrentItem() + 4;
         Log.d(TAG, "for expiryMinute ---> " + expiryMinute);
}

这可能是问题的原因。增加分钟值是否有任何具体原因?

答案 1 :(得分:0)

解决了设置分钟轮的值,如mins.setCurrentItem(expiryMinute-4);

final WheelView mins = (WheelView) view.findViewById(R.id.minutes);
            mins.setViewAdapter(new Mywheel(activity, 0, 59,"%02d"));
            mins.setCyclic(true);
            if(expiryMinute!=0){
            mins.setCurrentItem(expiryMinute-4);
            }