繁体   English   中英

从SecondActivity接收MainActivity中的字符串值,然后将其向下计数

[英]Receiving a string value in in MainActivity from SecondActivity, and then counting it down

在我的程序中,您在secondActivity中设置倒数计时器的字符串值。 然后将该值发送到MainActivities textView,然后应该在获取值后立即开始倒计时。 现在我已经设置了所以你可以设置值,并且值被正确获取,但是我不知道该怎么做是在收到它时开始倒数这个值。 我已经制作了一个CounterClass。 这是我的MainActivity Code ...

Bundle extras = getIntent().getExtras();

    String intentString;
    if(extras != null) {
        intentString = extras.getString("TimeValue");
        timer = new CounterClass(60000, 1000);
        timer.start();
        timeSent();

    } else {
        intentString = "Default String";
    }
    textTime= (TextView) findViewById(R.id.timeText);
    textTime.setText(intentString);

你需要启动你的计时器。 始终阅读文档

timer = new CounterClass(millisInFuture, countdownInterval);
timer.start();

编辑

millisInFuture ---从调用start()到倒计时完成并调用onFinish()的未来毫秒数。

countDownInterval ---接收onTick(long)回调的路径。

编辑18-12-2015

将您的intentString转换为millisInFuture并将其发送到CounterClass。 然后在onTick()方法onTick()其格式化为HH:MM。

String time = "16:54";
String split[] = time.split(":");
long futureInMillis = Integer.parseInt(split[0]) * 60 * 60 * 1000 + Integer.parseInt(split[1]) * 60 * 1000;

而不是这个

TextView timeText = (TextView)findViewById(R.id.timeText);
timeText.setText(intentString);

你需要这个

textTime= (TextView)findViewById(R.id.timeText);
textTime.setText(intentString);
timer = new CounterClass(10000, 1000);
timer.start();

您可能会看到timeText已更改为textTime,它是一个类成员变量,您的CounterClass使用它。

让我们说你真的想要实现自己的类。
我认为最好和最干净的方法是通过界面。

首先创建一个接口类。
AddTimes.java

public interface AddTimes  {
      void writeTime(String time);
}

现在,在您的班级中实现您的界面。

public class MainActivity extends AppCompatActivity implements AddTimes{

使用您的类TextView变量“textTime”

   timeText = (TextView)findViewById(R.id.timeText); //remove declaration TextView and use timeText instead of textTime . 

开始上课

new CounterClass(5000L,500L, this).start();

在CounterClass中,添加构造函数。

  AddTimes addTimes;
public CounterClass(long millisInFuture, long countDownInterval, AddTimes addTimes) {
    super(millisInFuture, countDownInterval);
    this.addTimes = addTimes;
}

替换textTime.setText(hms); to addTimes.writeTime(hms);

最后。 在你的主要活动。 实现AddTimes方法。

public void writeTime(String time) {
    timeText.setText("Time - " + time);
}

将代码原样复制到MainActivity.java文件中。
希望这可行。

public class MainActivity extends AppCompatActivity {

TextView textTime;
CountDownTimer timer;
int intValue = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if(extras != null) {
    String intentString = extras.getString("TimeValue"); //if this is a plain number like 20, 45, 209, 8
    intValue = Integer.valueOf(intentString);
}
textTime= (TextView)findViewById(R.id.timeText);
timer = new CountDownTimer(intValue * 1000, 1000) {
public void onTick(long millisUntilFinished) {
    intValue--;
    textTime.setText("Count Down: " + intValue);
}
@Override
public void onFinish() {}
}
}
timer.start();
}

您只需使用倒计时器:

  new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
            textField.setText("seconds remaining: " + millisUntilFinished / 1000);
           //here you can have your logic to set text to edittext
        }

        public void onFinish() {
            textField.setText("done!");
        }

    }.start();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM