我處於Android編程冒險的曙光之中,並且已經能夠在屏幕視圖之間進行通信。因此,下一步是成功從TextView(由Dialog設置)中拉取文本,並使用Start按鈕根據用戶選擇的對話框(默認爲時鐘的當前分鐘值)運行Timer。檢索TextView.getText()以設置一個開始按鈕的CountDownTimer
以下是您在screen上看到的內容。
- 一個TextView,顯示對話框中的選擇。
- 啓動對話框TimePicker對話框並重置開始按鈕的選擇器按鈕。
- 開始按鈕(應該)讀取TextView,禁用自身,並基於從TextView字符串中提取的Long開始CountDownTimer。
- 一個調試TextView,向我顯示系統實際看到的內容。
整個活動由一個Java文件組成,其中聲明瞭兩個類,當然還有一個XML。每次我點擊我的開始按鈕,儘管調試TextView顯示我正確地提取了長時間值的秒數計時器立即完成。我可以從我的調試TextView中看到,當我選擇說.. 08:26,pSecondsLeft = 26,因爲它應該..但計時器仍然不會從26倒數。我看不到我的錯誤。
這是XML的第一個。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView android:id="@+id/timeDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Time will appear here after being selected"
android:textSize="30sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="@+id/pickTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Change the time"/>
<Button android:id="@+id/startTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start the time"
/>
</LinearLayout>
<TextView android:id="@+id/timeRemaining"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:text="Time Remaining"
/>
</LinearLayout>
這是我的主要活動。
package com.stembo.android.botskooltimepickertutorial;
import java.util.Calendar;
import java.util.StringTokenizer;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class TimePickerActivity extends Activity {
/** Private members of the class */
private TextView displayTime;
private Button pickTime;
private Button startTimer;
private TextView timeRemaining;
private int pMinutesLeft;
private int pSecondsLeft;
/** This integer will uniquely define the
* dialog to be used for displaying time picker.*/
static final int TIME_DIALOG_ID = 0;
/** Callback received when the user "picks" a time in the dialog */
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int minLeft, int secLeft) {
pMinutesLeft = minLeft;
pSecondsLeft = secLeft;
updateDisplay();
displayToast();
}
};
/** Updates the time in the TextView */
private void updateDisplay() {
displayTime.setText(
new StringBuilder()
.append(pad(pMinutesLeft)).append(":")
.append(pad(pSecondsLeft)));
}
/** Displays a notification when the time is updated */
private void displayToast() {
Toast.makeText(this, new StringBuilder().append("Time choosen is ")
.append(displayTime.getText()), Toast.LENGTH_SHORT).show();
}
/** 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);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Capture our View elements */
displayTime = (TextView) findViewById(R.id.timeDisplay);
pickTime = (Button) findViewById(R.id.pickTime);
startTimer = (Button) findViewById(R.id.startTimer);
timeRemaining = (TextView) findViewById(R.id.timeRemaining);
/** Listener for click event of the pick button */
pickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startTimer.setEnabled(true);
showDialog(TIME_DIALOG_ID);
}
});
/**Listener for click event of the start button */
startTimer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startTimer.setEnabled(false);
StringTokenizer st = new StringTokenizer(displayTime.getText().toString(), ":");
while (st.hasMoreElements()){
st.nextElement();
long pSecondsTimer = Long.parseLong(st.nextToken());
}
timeRemaining.setText(displayTime.getText()+" Token="+ pSecondsLeft);
long oneSecondInterval = 1000;
MyCount counter = new MyCount(pSecondsLeft, oneSecondInterval);
counter.start();
}
});
/** Get the current time */
final Calendar cal = Calendar.getInstance();
pMinutesLeft = cal.get(Calendar.HOUR_OF_DAY);
pSecondsLeft = cal.get(Calendar.MINUTE);
/** Display the current time in the TextView */
updateDisplay();
}
/** Create a new dialog for time picker */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, pMinutesLeft, pSecondsLeft, true);
}
return null;
}
public class MyCount extends CountDownTimer {
public MyCount(long pSecondsLeft, long countDownInterval){
super(pSecondsLeft, countDownInterval);
}
@Override
public void onTick(long pSecondsTimer){
displayTime.setText("Time remaining: " + pSecondsLeft);
}
@Override
public void onFinish(){
displayTime.setText("Countdown Complete!");
}
}
}
這是我遇到的麻煩的開始按鈕的代碼,它的主要活動,但可能會更容易看到排除。
/**Listener for click event of the start button */
startTimer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startTimer.setEnabled(false);
StringTokenizer st = new StringTokenizer(displayTime.getText().toString(), ":");
while (st.hasMoreElements()){
st.nextElement();
long pSecondsTimer = Long.parseLong(st.nextToken());
}
timeRemaining.setText(displayTime.getText()+" Token="+ pSecondsLeft);
long oneSecondInterval = 1000;
MyCount counter = new MyCount(pSecondsLeft, oneSecondInterval);
counter.start();
}
});
你應該注意到,onCreateDialog被棄用減去。相反,你應該在FragmentManager中使用新的DialogFragment。 – Dave