无法从TimePicker检索时间参数

时间:2013-12-17 06:45:48

标签: android

我有一个DatePicker和TimePicker。我能够成功检索所有Date参数值。虽然我在使用TimePicker尝试相同时遇到错误,但该行显示错误“Add cast to timepicker”。我正在做什么有什么不对吗?

datepicker = (DatePicker) findViewById(R.id.pickerdate); 
timepicker = (TimePicker) findViewById(R.id.pickertime);

DateStr = datepicker.getDayOfMonth() + "/"
                + (datepicker.getMonth() + 1) + "/" + datepicker.getYear();

TimeStr = timepicker.getHourOfDay() + " : " + timepicker.getminute();

1 个答案:

答案 0 :(得分:1)

对于timePicker Widget,请使用以下函数: -

TimeStr = timepicker.getCurrentHour() + " : " + timepicker.getCurrentMinute();

使用我的TimePicker对话框。

单击按钮,将打开Time Picker Dialog。您选择时间,然后在按钮上将该时间设置为文本。 Xml只包含一个id为的按钮: - dueTime

您可以根据需要进行修改: -

package com.example.timepickerdialog;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;

public class MainActivity extends Activity {
    private int pHour;
    private int pMinute;
    static final int TIME_DIALOG_ID = 0;
    Button dueTime;
    private String AmPm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dueTime = (Button)findViewById(R.id.dueTime);
        dueTime.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });

    }

    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
        @Override    
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            pHour = hourOfDay;
            if(pHour>11){
                //pHour=pHour-12;
                AmPm="PM";
            }
            else
                AmPm="AM";
            pMinute = minute;

            dueTime.setText(
                    new StringBuilder()
                    .append(pad(pHour)).append(":")
                    .append(pad(pMinute)).append(" ")
                    .append(AmPm));
        }

    };
    /** Add padding to numbers less than ten */
    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {

        case TIME_DIALOG_ID:
            try {
                /** Get the current time */
                final Calendar cal = Calendar.getInstance();
                if(dueTime.getText().length()!=0){
                    String time[]=dueTime.getText().toString().split(":");
                    String time2[]=time[1].split(" ");
                    if(time2[1].trim().equalsIgnoreCase("AM")){
                        pHour = Integer.parseInt(time[0]);
                    }else{
                        pHour = Integer.parseInt(time[0])+12;
                    }
                    pMinute = Integer.parseInt(time2[0]);
                }else{
                    pHour = cal.get(Calendar.HOUR_OF_DAY);
                    pMinute = cal.get(Calendar.MINUTE);
                }
                return (new TimePickerDialog(this, mTimeSetListener, pHour, pMinute, true));
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}