2016-02-18 58 views
2

我處於Android編程冒險的曙光之中,並且已經能夠在屏幕視圖之間進行通信。因此,下一步是成功從TextView(由Dialog設置)中拉取文本,並使用Start按鈕根據用戶選擇的對話框(默認爲時鐘的當前分鐘值)運行Timer。檢索TextView.getText()以設置一個開始按鈕的CountDownTimer

以下是您在screen上看到的內容。

  1. 一個TextView,顯示對話框中的選擇。
  2. 啓動對話框TimePicker對話框並重置開始按鈕的選擇器按鈕。
  3. 開始按鈕(應該)讀取TextView,禁用自身,並基於從TextView字符串中提取的Long開始CountDownTimer。
  4. 一個調試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(); 
      } 
     }); 
+0

你應該注意到,onCreateDialog被棄用減去。相反,你應該在FragmentManager中使用新的DialogFragment。 – Dave

回答

3

看起來你對從TimePickerDialog.OnTimeSetListener得到的值有誤解。它給你幾個小時和幾分鐘的時間,但你只需要幾分鐘和幾秒鐘。您在onCreate上使用的日曆的值相同。

這就是說,如果你是仍然試圖用TimePickerDialog獲得分秒有充分的瞭解,你重新詮釋的價值觀,你將需要乘「秒」的號碼,你從接收選擇器減少1000以獲得一個以毫秒爲單位的單位,你可以將它提供給CountDownTimer。

MyCount counter = new MyCount(pSecondsLeft * 1000, oneSecondInterval); 
+0

是的,它確實給了小時和分鐘,所以我意識到通過重新解釋它們,我將自己限制在基於我的實現的24分59秒的計時器中,但我只是將它用作一小步來溝通事情I瞭解此刻。一旦我有這個下來,我的下一步將是用一些對話(或任何),可以達到99,或999等更換計時器。 我想我留下了一些東西從我的翻譯,我會去 - 根據您的意見進行評估並回報,謝謝! – Stembolt

0

我想這是因爲你沒有從pSecondsLeft變量對每個刻度

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); 
      pSecondsLeft --;   
     } 

     @Override 
     public void onFinish(){ 
      displayTime.setText("Countdown Complete!"); 
     } 
    }