0

我想把我的倒数计时器放在一个单独的方法中

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 。我仍然有不同的按钮,即按钮img1img8。我不知道我会使用什么样的方法。

我应该使用什么样的方法来存储我的 CountDownTimer?有点新的Android。请帮忙。感谢那些会有所帮助。

4

1 回答 1

0

If I understand what you want, you want to call the same method to start the CountDownTimer from different ImageButtons. If this is correct, just set the onClick in xml by adding a line like this to each ImageButton

<ImageButton
   ...
   android:onClick="someFunctino"/>   <!-- add this line to each -->

then in your java create this function

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

I don't quite understand your conditions so I can't help more with that but this way any Button clicked with that xml entry will call this function. If you need to know what Button was clicked you can use the id of v which will give you your Button id like

public void someFunction()
{
    int id = v.getId();  // can compare to id like if (id == R.id.buttonID)
    ...
}

And maybe I am misreading but you seem to have

if(txt.equals("Heart")){
// some code
else if(txt.equals("Heart")){

which is the same condition. If this isn't what you are talking about then please clarify a little.

Also, I think you need to rethink your conditions because I see a lot of repeated code and its a little confusing what's going on. And since you are always using the CountDownTimer you can just set it after your conditions one time. I don't understand why you say you can't use a method with a void return type.

于 2013-08-30T18:05:57.223 回答