2

これは、TimePicker 用に取得したコードです。

TimePicker.java

public class TimePicker extends EditText implements
    android.app.TimePickerDialog.OnTimeSetListener {
public interface OnTimeSetListener {
    public void onTimeSet(TimePicker view, int hour, int minute);
}

protected int hour;
protected int minute;
protected OnTimeSetListener onTimeSetListener;
protected java.text.DateFormat timeFormat;

public int getHour() {
    return hour;
}

public OnTimeSetListener getOnTimeSetListener() {
    return onTimeSetListener;
}

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

public void setHour(int hour) {
    this.hour = hour;
    updateText();
}

public int getMinute() {
    return minute;
}

public void setNow() {
    Calendar c = Calendar.getInstance();
    setTime(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
}

public void setMinute(int minute) {
    this.minute = minute;
    updateText();
}

public void setTime(int hour, int minute) {
    this.hour = hour;
    this.minute = minute;
    updateText();
}

public TimePicker(Context context, AttributeSet attrs) {
    super(context, attrs);

    timeFormat = DateFormat.getTimeFormat(getContext());

    setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
    setFocusable(false);
    setNow();
}

protected void showTimePicker() {
    TimePickerDialog timePickerDialog;

    timePickerDialog = new TimePickerDialog(getContext(), this, getHour(),
            getMinute(), true);
    timePickerDialog.show();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN)
        showTimePicker();

    return super.onTouchEvent(event);
}

public java.text.DateFormat getTimeFormat() {
    return timeFormat;
}

public void setTimeFormat(java.text.DateFormat timeFormat) {
    this.timeFormat = timeFormat;
    updateText();
}

@Override
public void onTimeSet(android.widget.TimePicker view, int hour, int minute) {
    // TODO Auto-generated method stub
    setTime(hour, minute);
    clearFocus();

    if (onTimeSetListener != null){
        onTimeSetListener.onTimeSet(this, hour, minute);
    }

}

public void updateText() {
    Calendar cal = new GregorianCalendar(0, 0, 0, getHour(), getMinute());
    setText(timeFormat.format(cal.getTime()));
}

}

calendar_events.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/maroon"
    tools:context="com.gpplsmje.mac.calendar.CalendarEvents" >

    <com.gpplsmje.mac.calendar.utils.TimePicker
        android:id="@+id/alarm_time"
        style="@android:style/Widget.Holo.Light.Spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="60"
        android:gravity="center"
        android:textColor="@color/white" />
    </RelativeLayout>

CalendarEvents.java

public class CalendarEvents extends Activity implements OnClickListener {
    TimePicker notifyTime;

Button back, cancel, done;
int hour, minute, day, month, year;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calendar_events);
    onNewIntent(getIntent());

    notifyTime = ((TimePicker) findViewById(R.id.alarm_time));
    notifyTime.setTimeFormat(DateFormat.getTimeFormat(this));

    hour = notifyTime.getHour();
    minute = notifyTime.getdMinute();

    done.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    switch (v.getId()){
    case R.id.bDone:
        Toast.makeText(this, year + " " + month + " " + day + " " + hour + " " + minute , Toast.LENGTH_LONG).show();
        break;
    }
}

から選択したものが正しいToastかどうかを確認するメッセージを作成しました。TimePickerしかし、このコードを実行するたびに、 から選択した時間ではなく、マシンの現在の時間が常に表示されますTimePicker。どうすればこれを達成できますか? 誰でも私を助けてもらえますか?前もって感謝します。

4

4 に答える 4

2

この方法を使用するsetOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)

タイムピッカーを使用するときのこのクラス。 タイムピッカー

その上でonclickリスナーではありません..

于 2013-09-04T05:49:30.680 に答える
2

ハンドラーでTimePicker からフォーカスをクリアするとonClick()、そのパブリックメソッドを使用して、選択した時間値を整数として取得できます。

public void onClick(View v) {
    ...
    notifyTime = ((TimePicker) findViewById(R.id.alarm_time));
    notifyTime.clearFocus();

    hour = notifyTime.getCurrentHour();
    minute = notifyTime.getCurrentMinute();
    ....
}
于 2013-09-04T06:12:05.853 に答える
0

ユーザーが入力した時間を読み取るには、リスナーを使用します

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
    new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mHour = hourOfDay;
            mMinute = minute;
            updateDisplay();
        }
    };
于 2013-09-04T05:45:03.190 に答える
0

こうすれば時間が取れる

Calendar mCalendar = Calendar.getInstance();

 mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
 mMinute = mCalendar.get(Calendar.MINUTE);

 TimePickerDialog timePickerDialog = new TimePickerDialog(mActivity, mTimeSetListener, mHour, mMinute, false);

 private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

         mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
         mCalendar.set(Calendar.MINUTE, mMinute);
         SimpleDateFormat mSDF = new SimpleDateFormat("hh:mm a");
         String time = mSDF.format(mCalendar.getTime());
     }
 }
于 2013-09-04T06:33:43.063 に答える