2014-07-17 173 views
0

在Android項目我有這個類的Java TimePicker不返回正確的值

package mycalories.com.jalle.mycalories; 

import android.app.Dialog; 
import android.app.DialogFragment; 
import android.app.TimePickerDialog; 
import android.os.Bundle; 
import android.text.format.DateFormat; 
import android.text.format.Time; 
import android.widget.TimePicker; 

import java.util.Calendar; 

public class TimePickerFragment extends DialogFragment implements 
     TimePickerDialog.OnTimeSetListener { 

    public static interface OnTimeSetListener { 
     void onTimeSet(Time time); 
    } 

    private OnTimeSetListener onTimeSetListener; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     final Calendar c = Calendar.getInstance(); 
     int hour = c.get(Calendar.HOUR_OF_DAY); 
     int minute = c.get(Calendar.MINUTE); 

     return new TimePickerDialog(getActivity(), this, hour, minute, 
       DateFormat.is24HourFormat(getActivity())); 
    } 

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     if (onTimeSetListener != null) { 
      Time newTime =new Time(hourOfDay+":"+minute+ ""); 
      onTimeSetListener.onTimeSet(newTime); 
     } 
    } 

    public void setOnTimeSetListener(OnTimeSetListener onTimeSetListener) { 
     this.onTimeSetListener = onTimeSetListener; 
    } 
} 

應該返回選定時間。而在主要活動我有按鈕和文本字段:

Button btnTime = (Button) findViewById(R.id.btnTime); 
     btnTime.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       TimePickerFragment timePicker = new TimePickerFragment(); 
       timePicker.show(getFragmentManager(), "timePicker"); 
       timePicker.setOnTimeSetListener(new OnTimeSetListener() { 

        @Override 
        public void onTimeSet(Time time) { 

        // SimpleDateFormat HHMM = new SimpleDateFormat("hh:mm"); 

         final EditText txtTime = (EditText) findViewById(R.id.txtTime); 
         txtTime.setText(time.toString()); 
        } 
       }); 
      } 
     }); 

選擇時間後,我應該在hh:mm格式,而是我19700101T00000011:55(4,0,0,0,0) (顯然當前時間沒有選擇,但默認的一個) 我想方法onTimeSet是不正確的。任何想法 ?

還有一個問題:如何在diff活動中爲多個文本框使用相同的TimePicker類?

回答

1

Time API有3個構造函數:無參數,String參數,以及其他Time參數。您正在使用的構造函數,這是一個String,只接受時區的預期值,這將日期設置爲1月1日,1970年

公開時間(字符串時區)

構建時區中的時間對象由 命名的字符串參數「timezone」。時間被初始化到1月1日, 1970年

如果你想要回Time今天的日期,選定的時間,嘗試改變代碼

public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
    if (onTimeSetListener != null) { 
     Time newTime = new Time(); 
     newTime.setToNow(); 
     newTime.hour = hourOfDay; 
     newTime.minute = minute; 
     onTimeSetListener.onTimeSet(newTime); 
    } 
} 
1

知道你最終想要顯示在hh:mm格式的時間,使用java.util.Date類來實現您的界面方法會更容易,其中SimpleDateFormat「玩得很好」。在TimePickerFragment類,更改以下:

public static interface OnTimeSetListener { 
    void onTimeSet(Date date); 
} 

public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
    if (onTimeSetListener != null) { 
     Date date = new Date(); 
     date.setHours(hourOfDay); 
     date.setMinutes(minute); 
     onTimeSetListener.onTimeSet(date); 
    } 
} 

而在onClick()方法:

public void onClick(View v) 
{ 
    TimePickerFragment timePicker = new TimePickerFragment(); 
    timePicker.show(getFragmentManager(), "timePicker"); 
    timePicker.setOnTimeSetListener(new TimePickerFragment.OnTimeSetListener() { 
      @Override 
      public void onTimeSet(Date date) 
      { 
       final EditText txtTime = (EditText) findViewById(R.id. txtTime); 
       SimpleDateFormat HHMM = new SimpleDateFormat("hh:mm"); 
       txtTime.setText(HHMM.format(date)); 
      } 
     } 
    ); 
}