我想将CountDown计时器放在一个单独的方法中
img9.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
img9.setBackgroundResource(myImg9);
String txt = PName.getText().toString();
if(txt.equals("Heart")){
if(myImg9 == R.drawable.heart){
correct++;
img9.setBackgroundResource(myImg9);
new CountDownTimer(1000,1000) {
public void onTick(long millisUntilFinished) {
//This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
// After the time is experied so here can change image
img9.setBackgroundResource(android.R.drawable.btn_default);
img9.setEnabled(false);
}
}.start();
}
//wrong heart
else if(txt.equals("Heart")){
if(myImg9== R.drawable.circle || myImg9== R.drawable.square || myImg9== R.drawable.triangle){
img9.setBackgroundResource(myImg9);
new CountDownTimer(1000,1000) {
public void onTick(long millisUntilFinished) {
//This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
// After the time is experied so here can change image
img9.setBackgroundResource(android.R.drawable.btn_default);
}
}.start();
}
}
}
if(txt.equals("Circle")){
if(myImg9 == R.drawable.circle){
correct++;
img9.setBackgroundResource(myImg9);
new CountDownTimer(1000,1000) {
public void onTick(long millisUntilFinished) {
//This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
// After the time is experied so here can change image
img9.setBackgroundResource(android.R.drawable.btn_default);
img9.setEnabled(false);
}
}.start();
}
//wrong circle
else if(txt.equals("Circle")){
if(myImg9== R.drawable.heart || myImg9== R.drawable.square || myImg9== R.drawable.triangle){
img9.setBackgroundResource(myImg9);
new CountDownTimer(1000,1000) {
public void onTick(long millisUntilFinished) {
//This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
// After the time is experied so here can change image
img9.setBackgroundResource(android.R.drawable.btn_default);
}
}.start();
}
}
}
if(txt.equals("Triangle")){
if(myImg9 == R.drawable.triangle){
correct++;
img9.setBackgroundResource(myImg9);
new CountDownTimer(1000,1000) {
public void onTick(long millisUntilFinished) {
//This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
// After the time is experied so here can change image
img9.setBackgroundResource(android.R.drawable.btn_default);
img9.setEnabled(false);
}
}.start();
}
//wrong circle
else if(txt.equals("Triangle")){
if(myImg9== R.drawable.heart || myImg9== R.drawable.square || myImg9== R.drawable.circle){
img9.setBackgroundResource(myImg9);
new CountDownTimer(1000,1000) {
public void onTick(long millisUntilFinished) {
//This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
// After the time is experied so here can change image
img9.setBackgroundResource(android.R.drawable.btn_default);
}
}.start();
}
}
}
if(txt.equals("Square")){
if(myImg9 == R.drawable.square){
correct++;
img9.setBackgroundResource(myImg9);
new CountDownTimer(1000,1000) {
public void onTick(long millisUntilFinished) {
//This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
// After the time is experied so here can change image
img9.setBackgroundResource(android.R.drawable.btn_default);
img9.setEnabled(false);
}
}.start();
}
//wrong circle
else if(txt.equals("Square")){
if(myImg9== R.drawable.heart || myImg9== R.drawable.circle || myImg9== R.drawable.triangle){
img9.setBackgroundResource(myImg9);
new CountDownTimer(1000,1000) {
public void onTick(long millisUntilFinished) {
//This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
// After the time is experied so here can change image
img9.setBackgroundResource(android.R.drawable.btn_default);
}
}.start();
}
}
}
}
});
正如您所看到的,我在很多条件下调用了一个新的CountDownTimer。但是,我想把它放在一个单独的方法中,所以我可以调用它来使我的java文件更具可读性。但是,我不能把它放在void方法中,因为我有不同的按钮。在这里,我在 img9 上使用了CountDownTimer。我仍然有不同的按钮,即按钮 img1 到 img8 。我不知道我会用什么样的方法。
我应该使用哪种方法来存储我的CountDownTimer?有点新的Android。请帮忙。感谢他们的帮助。
答案 0 :(得分:0)
如果我了解您的需求,您需要使用相同的方法从不同的CountDownTimer
启动ImageButtons
。如果这是正确的,只需在xml中设置onClick
,方法是在每个ImageButton
<ImageButton
...
android:onClick="someFunctino"/> <!-- add this line to each -->
然后在你的java中创建这个函数
public void someFunction(View v)
{
// conditions
new CountDownTimer(1000,1000) {
public void onTick(long millisUntilFinished) {
//This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
// After the time is experied so here can change image
img9.setBackgroundResource(android.R.drawable.btn_default);
img9.setEnabled(false);
}
}.start();
}
我不太了解你的情况,所以我对此无能为力,但这样任何用{x}条目点击的Button
都会调用此函数。如果您需要知道点击Button
的内容,可以使用id
v
Button id
来public void someFunction()
{
int id = v.getId(); // can compare to id like if (id == R.id.buttonID)
...
}
if(txt.equals("Heart")){
// some code
else if(txt.equals("Heart")){
也许我在误读,但你似乎已经
了CountDownTimer
这是相同的条件。如果这不是你所说的那么请澄清一点。
此外,我认为您需要重新考虑您的条件,因为我看到了很多重复的代码,而且有点混淆了正在发生的事情。由于您始终使用void
,因此您可以在条件一次后设置它。我不明白为什么你说你不能使用{{1}}返回类型的方法。