从另一个类获取 TextView 用于倒计时。

huangapple go评论72阅读模式
英文:

Get TextView from another class for countdown

问题

我已经创建了一个类来进行倒计时,并将其用于将格式化的时间返回为TextView。然而,我无法使时间显示出来。当我不是调用getTime()方法而是使用一个字符串时,它可以正常显示。

主要方法如下:

setContentView(R.layout.activity_main);

matchTime = findViewById(R.id.match_Time);

playTime = new Play();
matchTime.setText(playTime.getPlayTime());

而我的Play类如下:

//其他实现

private void updatePlay() {
      int minutes = (int) (timeUntilEnd / 1000) / 60;
      int seconds = (int) (timeUntilEnd / 1000) % 60;

     timeUntilEndFormated = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
     playTime.setText(timeUntilEndFormated);
}

public TextView getPlayTime() {
    return playTime;
}
英文:

I've created a class to make a countdown and used it to return the formated time as a TextView. However, I'm not beeing able to get the time to be exibed on in place. When, instead of calling the method getTime() I use a string, it shows fine.

The main method is like this:

setContentView(R.layout.activity_main);

matchTime = findViewById(R.id.match_Time);

playTime = new Play();
matchTime.setText(playTime.getPlayTime());

While my class Play goes like this:

//Other implementations

    private void updatePlay() {
          int minutes = (int) (timeUntilEnd/ 1000) / 60;
          int seconds = (int) (timeUntilEnd/ 1000) % 60;

         timeUntilEndFormated= String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);
         playTime.setText(timeUntilEndFormated);
    }

    public TextView getPlayTime() {
        return playTime;
    }


答案1

得分: 0

看起来你正在将matchTime的文本设置为一个TextView。
setText是一个应该接收String而不是TextView对象的方法。

但是,如果你只是在getPlayTime()中简单地返回一个String,updatePlay()不起作用,因为playTime.setText()不会影响matchTime。

为了解决这个问题,你可以将matchTime传递给Play的构造函数,然后直接更新它:

public class Play{
    
    private TextView matchTime;

    public Play(TextView matchTime){
        this.matchTime = matchTime;
    }

     private void updatePlay() {
       int minutes = (int) (timeUntilEnd/ 1000) / 60;
       int seconds = (int) (timeUntilEnd/ 1000) % 60;

       timeUntilEndFormated= String.format(Locale.getDefault(),"%%02d:%%02d", minutes, seconds);
       matchTime.setText(timeUntilEndFormated);
     }
}

然后你只需要在主方法中这样做:

playTime = new Play(matchTime);
playTime.updatePlay();

在将TextView传递给其他对象时要小心。这可能会导致内存泄漏,因为TextView持有你的Context的实例。为了避免内存泄漏,你必须在activity的onDestroy()方法中释放对象:

public void onDestroy() {
    super.onDestroy();
    playTime = null;
}
英文:

It looks like you are setting the text of matchTime to a TextView.
setText is a method that should receive a String, not a TextView object.

But, if you simply return a String in getPlayTime(), updatePlay() wont work because playTime.setText() wont affect matchTime at all.

To solve this you pass matchTime to Play's constructor, and then update it directly:

public class Play{
    
    private TextView matchTime;

    public Play(TextView matchTime){
        this.matchTime = matchTime;
    }

     private void updatePlay() {
       int minutes = (int) (timeUntilEnd/ 1000) / 60;
       int seconds = (int) (timeUntilEnd/ 1000) % 60;

       timeUntilEndFormated= String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);
       matchTime.setText(timeUntilEndFormated);
     }
}

And then you only need to do this in your main method:

playTime = new Play(matchTime);
playTime.updatePlay();

Be careful with passing TextView's to other objects. This can create a memory leak, since TextView holds an instance of your Context. To avoid memory leaks you have to release the object in your activity's onDestroy() method:

 public void onDestroy() {
    super.onDestroy();
    playTime = null;
 }

huangapple
  • 本文由 发表于 2020年8月15日 02:07:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63417966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定