我希望进行一些更改,以便不必重复代码(有意义)!

这是我目前拥有的:

package com.rcd.learningactivities;

import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final TextView blueTimer = (TextView) findViewById(R.id.blueTimer);
        Button blue = (Button) findViewById(R.id.button1);

        final TextView redTimer = (TextView) findViewById(R.id.redTimer);
        Button red = (Button) findViewById(R.id.button2);



        final CountDownTimer cd = new CountDownTimer(300000, 1000) {

             public void onTick(long millisUntilFinished) {
                    DecimalFormat dfmin = new DecimalFormat("0");
                    DecimalFormat dfsec = new DecimalFormat("00");
                    double seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)%60;
                    double min = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
                    blueTimer.setText(String.valueOf(dfmin.format(min)) + ":" + String.valueOf(dfsec.format(seconds)));

             }

             public void onFinish() {
                 blueTimer.setText("5:00");
             }
          };

        blue.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (blueTimer.getText().toString().contains("5:00")){
                    cd.start();

                } else {
                    cd.cancel();
                    cd.onFinish();
                }

            }
        });

        red.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (redTimer.getText().toString().contains("5:00")){
                    cd.start();
                } else {
                    cd.cancel();
                    cd.onFinish();
                }

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


我希望blue.setOnClickListener和red.setOnClickListener都能够利用我的cd CountDownTimer,但是我需要blueTimer.setText(String ...)才能基于任何一个更改为redTimer.setText(String ...)用户单击的按钮。

我真的希望这是有道理的,如果我倒想一下,请告诉我。

本质上,我想在上面的onTick方法中使blueTimer成为变量x。

最佳答案

关键是要使用类字段或属性:

    public class MainActivity extends Activity {
    protected CountDownTimer cd;
    private Button lastPressedButton;
    private Button red;
    private Button blue;
    private TextView blueTimer;
    private TextView redTimer;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            blueTimer = (TextView) findViewById(R.id.blueTimer);
            blue = (Button) findViewById(R.id.button1);

            redTimer = (TextView) findViewById(R.id.redTimer);
            red = (Button) findViewById(R.id.button2);



            cd = new CountDownTimer(300000, 1000) {

                 public void onTick(long millisUntilFinished) {
                        DecimalFormat dfmin = new DecimalFormat("0");
                        DecimalFormat dfsec = new DecimalFormat("00");
                        double seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)%60;
                        double min = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
if (lastPressedButton == red){
                 redTimer.setText(String.valueOf(dfmin.format(min)) + ":" + String.valueOf(dfsec.format(seconds)));
}
else if (lastPressedButton == blue){
                 blueTimer.setText(String.valueOf(dfmin.format(min)) + ":" + String.valueOf(dfsec.format(seconds)));
}


                 }

                 public void onFinish() {
    if (lastPressedButton == red)
                     blueTimer.setText("5:00");
    else if (lastPressedButton == blue){
    //something else?
    }
                 }
              };

            blue.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
    lastPressedButton = blue;
                    if (blueTimer.getText().toString().contains("5:00")){
                        cd.start();

                    } else {
                        cd.cancel();
                        cd.onFinish();
                    }

                }
            });

            red.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
    lastPressedButton = red;
                    if (redTimer.getText().toString().contains("5:00")){
                        cd.start();
                    } else {
                        cd.cancel();
                        cd.onFinish();
                    }

                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }

10-08 03:08