我对“countDownTimer”有一个问题,当我在listview中的两个不同的行中运行两个计数器时,“onfinish”函数仅针对最后一次单击的行继续进行..
所以,“onfinish”方法会显示对话框,并显示最后一个文本视图的文本。
但是,我希望“onfinish”方法显示已完成计数的特定点击行的对话框。
这是代码
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class RecipeStepsActivity extends Activity {
ListView list;
int minutes;
int seconds;
int hours;
TextView fullTime;
TextView step;
String fullTimeString;
String[] fullTimeArray;
MediaPlayer myTen;
Activity context;
long init = 0, now;
AdapterView<?> parent1;
CountDownTimer x;
static boolean[] arr;
@SuppressLint("ShowToast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_steps);
context = this;
arr = new boolean[XmlHandler.HashMapOfTime.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = false;
}
System.out.println(XmlHandler.HashMapOfTime.size()
+ "++++++++++++++++++++++");
list = (ListView) findViewById(R.id.listId);
list.setAdapter(new RecipeStepsListAdapter(RecipeStepsActivity.this,
R.layout.activity_recipe_steps_list_adapter,
XmlHandler.HashMapOfSteps.get(Main.position),
XmlHandler.HashMapOfTime.get(Main.position)));
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int newid,
long arg3) {
parent1 = parent;
final String item = (String) parent1.getItemAtPosition(newid);
fullTimeArray = new String[3];
fullTimeArray = item.split(":");
hours = Integer.parseInt(fullTimeArray[0]);
minutes = Integer.parseInt(fullTimeArray[1]);
seconds = Integer.parseInt(fullTimeArray[2]);
Long times = ((hours * 60L * 60L) + (minutes * 60L) + seconds) * 1000;
for (int i = 0; i < arr.length; i++) {
if (i == newid && arr[newid] == false) {
Toast.makeText(RecipeStepsActivity.this, "Counter begin",
Toast.LENGTH_SHORT).show();
createCounterDownTimer(times, newid);
arr[newid] = true;
break;
} else if (i == newid && arr[newid] == true) {
Toast.makeText(RecipeStepsActivity.this,
"Counter stopped", Toast.LENGTH_SHORT).show();
arr[newid] = false;
x.cancel();
break;
}
}
}
});
}
public void createCounterDownTimer(final Long times, final int id) {
runOnUiThread(new Runnable() {
public void run() {
x = new CountDownTimer(times, 1000) {
@SuppressLint("NewApi")
@Override
public void onTick(long ms) {
// System.out.println("===");
fullTime = (TextView) findViewById(id);
step = (TextView) findViewById(id + 10);
System.out.println(step.getId()+" of the onTick step");
// step.setId(id);
System.out.println(step.getId()+" -------------------------"+step.getText()+"---------------------- ");
System.out.println(fullTime.getId()+" -------------------------"+fullTime.getText()+"---------------------- ");
int seconds = (int) (ms / 1000) % 60;
int minutes = (int) ((ms / (1000 * 60)) % 60);
int hours = (int) ((ms / (1000 * 60 * 60)) % 24);
System.out.println(id+" ===> "+hours + ": " + minutes +
": "
+ seconds);
fullTime.setText(hours + ": " + minutes + ": "
+ seconds);
}
@SuppressWarnings("deprecation")
@Override
public void onFinish() {
// TODO Auto-generated method stub
fullTime.setText("time up!");
System.out.println(step.getId()+" finished id step");
System.out.println(fullTime.getId()
+ " =============== " + fullTime.getText()
+ "===" + step.getText());
myTen = MediaPlayer.create(RecipeStepsActivity.this,
R.raw.soundd);
myTen.start();
AlertDialog alertDialog = new AlertDialog.Builder(
RecipeStepsActivity.this).create();
alertDialog.setTitle("time up");
alertDialog.setMessage(fullTime.getText().toString()
+ " is finished");
alertDialog.setButton("ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method
// stub
System.out.println(step.getId()+" onclick dialog finished id step");
System.out.println("finished");
myTen.stop();
dialog.cancel();
}
});
alertDialog.show();
}
}.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.recipe_steps, menu);
return true;
}
}
答案 0 :(得分:1)
我通过在ontick()方法中添加条件来解决问题
在里面我放了一个变量来保存当前计数的值(步骤文本)。 我在onfinish()函数中使用过它。
这是代码
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class RecipeStepsActivity extends Activity {
ListView list;
int minutes;
int seconds;
int hours;
TextView fullTime;
TextView step;
String fullTimeString;
String[] fullTimeArray;
MediaPlayer myTen;
Activity context;
long init = 0, now;
AdapterView<?> parent1;
CountDownTimer x;
static boolean[] arr;
@SuppressLint("ShowToast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_steps);
context = this;
arr = new boolean[XmlHandler.HashMapOfTime.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = false;
}
list = (ListView) findViewById(R.id.listId);
list.setAdapter(new RecipeStepsListAdapter(RecipeStepsActivity.this,
R.layout.activity_recipe_steps_list_adapter,
XmlHandler.HashMapOfSteps.get(Main.position),
XmlHandler.HashMapOfTime.get(Main.position)));
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int newid,
long arg3) {
parent1 = parent;
final String item = (String) parent1.getItemAtPosition(newid);
fullTimeArray = new String[3];
fullTimeArray = item.split(":");
hours = Integer.parseInt(fullTimeArray[0]);
minutes = Integer.parseInt(fullTimeArray[1]);
seconds = Integer.parseInt(fullTimeArray[2]);
Long times = ((hours * 60L * 60L) + (minutes * 60L) + seconds) * 1000;
for (int i = 0; i < arr.length; i++) {
if (i == newid && arr[newid] == false) {
Toast.makeText(RecipeStepsActivity.this, "Counter begin",
Toast.LENGTH_SHORT).show();
createCounterDownTimer(times, newid);
arr[newid] = true;
break;
} else if (i == newid && arr[newid] == true) {
Toast.makeText(RecipeStepsActivity.this,
"Counter stopped", Toast.LENGTH_SHORT).show();
arr[newid] = false;
x.cancel();
break;
}
}
}
});
}
String temp ;
public void createCounterDownTimer(final Long times, final int id) {
runOnUiThread(new Runnable() {
public void run() {
x = new CountDownTimer(times, 1000) {
@SuppressLint("NewApi")
@Override
public void onTick(long ms) {
// System.out.println("===");
fullTime = (TextView) findViewById(id);
step = (TextView) findViewById(id + 10);
int seconds = (int) (ms / 1000) % 60;
int minutes = (int) ((ms / (1000 * 60)) % 60);
int hours = (int) ((ms / (1000 * 60 * 60)) % 24);
fullTime.setText(hours + ": " + minutes + ": "
+ seconds);
if(hours == 0 && minutes == 0 && seconds == 1){
temp = step.getText().toString();
fullTime.setText("time up!");
}
}
@SuppressWarnings("deprecation")
@Override
public void onFinish() {
// TODO Auto-generated method stub
myTen = MediaPlayer.create(RecipeStepsActivity.this,
R.raw.soundd);
myTen.start();
AlertDialog alertDialog = new AlertDialog.Builder(
RecipeStepsActivity.this).create();
alertDialog.setTitle("time up");
alertDialog.setMessage(temp
+ " is finished");
//arrayyy.remove(0);
alertDialog.setButton("ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method
myTen.stop();
dialog.cancel();
}
});
alertDialog.show();
}
}.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.recipe_steps, menu);
return true;
}
}
答案 1 :(得分:0)
好的,根据您的要求,这样做更简单,更干净。
如果您要取消任何消息,只需从处理程序中调用removeCallback即可撤消特定回拨。
在handleMessage处理程序块中写入对话框弹出代码,在那里你可以弹出一个包含所需消息的警告框。
希望有所帮助。