2016-02-18 19 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!"); 
     } 
    }