检索 TextView.getText() 以设置带有开始按钮的 CountDownTimer
Retrieving TextView.getText() to set a CountDownTimer with a Start Button
我正处于 Android 编程冒险的开端,刚刚能够在屏幕视图之间进行交流。所以我的下一步是成功地从 TextView 中提取文本(由对话框设置)并使用开始按钮 运行 基于用户 selected 对话框的计时器(默认为当前分钟时钟的值)。
这是您在 screen 上看到的内容。
- 显示对话框中 selection 的 TextView。
- 启动对话框 TimePicker 对话框并重置启动按钮的选择器按钮。
- 一个开始按钮,它(应该)读取 TextView、禁用自身并根据从 TextView 字符串中提取的 Long 开始一个 CountDownTimer。
- 一个调试 TextView,向我显示系统实际看到的内容。
整个 activity 由一个 Java 文件组成,其中声明了两个 类,当然还有一个 XML。每次我单击我的开始按钮时,尽管 Debug TextView 显示我正确地提取了计时器立即完成的秒数的 Long 值。我可以从我的调试 TextView 中看到,当我 select 说.. 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>
这是我的主要 Activity。
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!");
}
}
}
这是我遇到问题的“开始”按钮代码,它在主程序中 Activity 但可能更容易被排除在外。
/**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();
}
});
您从 TimePickerDialog.OnTimeSetListener 返回的值似乎存在误解。它给你小时和分钟,但你期待的是分钟和秒。对于来自您在 onCreate 上使用的日历的值也是如此。
也就是说,如果您仍然尝试使用 TimePickerDialog 来获取分钟和秒,并且完全理解您正在重新解释这些值,则需要乘以"seconds" 您从选择器收到的数量增加 1000 以获得一个可以提供给 CountDownTimer 的毫秒单位。
MyCount counter = new MyCount(pSecondsLeft * 1000, oneSecondInterval);
我认为这是因为您没有在每次报价时从 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!");
}
}
我正处于 Android 编程冒险的开端,刚刚能够在屏幕视图之间进行交流。所以我的下一步是成功地从 TextView 中提取文本(由对话框设置)并使用开始按钮 运行 基于用户 selected 对话框的计时器(默认为当前分钟时钟的值)。
这是您在 screen 上看到的内容。
- 显示对话框中 selection 的 TextView。
- 启动对话框 TimePicker 对话框并重置启动按钮的选择器按钮。
- 一个开始按钮,它(应该)读取 TextView、禁用自身并根据从 TextView 字符串中提取的 Long 开始一个 CountDownTimer。
- 一个调试 TextView,向我显示系统实际看到的内容。
整个 activity 由一个 Java 文件组成,其中声明了两个 类,当然还有一个 XML。每次我单击我的开始按钮时,尽管 Debug TextView 显示我正确地提取了计时器立即完成的秒数的 Long 值。我可以从我的调试 TextView 中看到,当我 select 说.. 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>
这是我的主要 Activity。
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!");
}
}
}
这是我遇到问题的“开始”按钮代码,它在主程序中 Activity 但可能更容易被排除在外。
/**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();
}
});
您从 TimePickerDialog.OnTimeSetListener 返回的值似乎存在误解。它给你小时和分钟,但你期待的是分钟和秒。对于来自您在 onCreate 上使用的日历的值也是如此。
也就是说,如果您仍然尝试使用 TimePickerDialog 来获取分钟和秒,并且完全理解您正在重新解释这些值,则需要乘以"seconds" 您从选择器收到的数量增加 1000 以获得一个可以提供给 CountDownTimer 的毫秒单位。
MyCount counter = new MyCount(pSecondsLeft * 1000, oneSecondInterval);
我认为这是因为您没有在每次报价时从 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!");
}
}