【问题标题】:Looking to create a generic variable (i hope that is the right term)希望创建一个通用变量(我希望这是正确的术语)
【发布时间】:2014-01-22 21:14:01
【问题描述】:

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

这是我现在所拥有的:

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。

【问题讨论】:

    标签: java android variables


    【解决方案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;
            }
    
        }
    

    【讨论】:

    • 谢谢你,这个答案对我来说最有意义,我现在已经开始工作了!很有可能有更好的答案,但由于我还是新手,这个最容易理解!谢谢
    【解决方案2】:
    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);
    

    【讨论】:

    • 您对我的回答抛出异常是正确的。我删除了我的答案,因为您的答案更好地涵盖了这种情况。
    • 除了……这与他的要求无关。
    【解决方案3】:

    最简单的答案是在计时器中设置要更新的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,并且在您的点击监听器中您可以在调用start()之前调用setViewToUpdate()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 2012-01-31
      • 2023-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多