期待创建一个通用变量(我希望这是正确的术语)

时间:2014-01-22 21:14:01

标签: java android variables

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

以下是我现在的情况:

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。

3 个答案:

答案 0 :(得分:1)

重点是使用类字段或属性:

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

    }

答案 1 :(得分:1)

OnClickListener clickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {

        TextView tv = null;
        switch(v.getId()){
          case R.id.button1:
              tv = blueTimer;
            break;
          case R.id.button2:
               tv = redTimer;
             break;
}
            if (tv.getText().toString().contains("5:00") {
                cd.start();
            } else {
                cd.cancel();
                cd.onFinish();
            }
        }
    }

blue.setOnClickListener(clickListener);
red.setOnClickListener(clickListener);

答案 2 :(得分:0)

最简单的答案是设置要在计时器中更新的TextView然后启动计时器。这需要您使用自己的类扩展CountDownTimer

public class MyCountDownTimer extends CountDownTimer {

    private TextView viewToUpdate; 

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

        // Make sure a view has been set
        if (viewToUpdate != null) {
            viewToUpdate.setText(String.valueOf(dfmin.format(min)) + ":" + 
                                 String.valueOf(dfsec.format(seconds)));
        }   
     }

     public void onFinish() {
         if (viewToUpdate != null) {
             viewToUpdate.setText("5:00");
         }
     }

     public void setViewToUpdate(TextView v)
     {
         viewToUpdate = v;
     }
}

现在您可以使用{而不是CountDownTimer代替setViewToUpdate(),在点击监听器中,您可以在致电start()之前致电{{1}}