假設你使用的是Spinner來獲取用戶輸入的時候,有你考慮使用TimePicker還是TimePickerDialog?
以下幾段代碼已經從時間選取器教程中獲得。我只粘貼了相關的方法來說明我的觀點,但完整的教程可以發現here:
public class HelloTimePickerActivity extends Activity {
private TextView mTimeDisplay;
private Button mPickTime;
static final int TIME_DIALOG_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// capture our View elements
mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
mPickTime = (Button) findViewById(R.id.pickTime);
// add a click listener to the button
mPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
// snip...
}
// the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
}
return null;
}
}
正如你所看到的,回調onTimeSet()
傳遞小時和分鐘爲int
值,容易你去檢查。
什麼是「模塊」?你想做什麼?如果(s == 17:00)不是有效的java/android代碼。 – claesv 2012-04-24 13:50:51
你如何期待「:」被解析爲一個數字......?在發佈這類問題之前,你應該真正處理你的編程技巧。 – 2012-04-24 13:50:58
我已經發布了一個答案,但我傾向於同意 - 即使在編輯之後,措辭也很差。 – OnResolve 2012-04-24 13:58:00