简体   繁体   English

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

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

In my program you set the string value for the countdown timer in the secondActivity. 在我的程序中,您在secondActivity中设置倒数计时器的字符串值。 That value is then sent to the MainActivities textView which is then supposed to start counting down as soon as the value is fetched. 然后将该值发送到MainActivities textView,然后应该在获取值后立即开始倒计时。 Right now I have made it so you can set the value, and the value is fetched correctly, but what I don't know how to do is start Counting Down this value when It is received. 现在我已经设置了所以你可以设置值,并且值被正确获取,但是我不知道该怎么做是在收到它时开始倒数这个值。 I have already made a CounterClass. 我已经制作了一个CounterClass。 Here is my MainActivity Code... 这是我的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);

You need to start your timer. 你需要启动你的计时器。 Always read the docs . 始终阅读文档

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

EDIT 编辑

millisInFuture --- The number of millis in the future from the call to start() until the countdown is done and onFinish() is called. millisInFuture ---从调用start()到倒计时完成并调用onFinish()的未来毫秒数。

countDownInterval --- The interval along the way to receive onTick(long) callbacks. countDownInterval ---接收onTick(long)回调的路径。

EDIT 18-12-2015 编辑18-12-2015

Convert your intentString to millisInFuture and send it to the CounterClass. 将您的intentString转换为millisInFuture并将其发送到CounterClass。 And then format it back to HH:MM in onTick() method. 然后在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;

Instead of this 而不是这个

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

you need this 你需要这个

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

As you might see timeText has changed to textTime which is a class member variable and your CounterClass uses it. 您可能会看到timeText已更改为textTime,它是一个类成员变量,您的CounterClass使用它。

Lets say that you really want to implement your own class. 让我们说你真的想要实现自己的类。
I think the best and clean way to do it is by interface. 我认为最好和最干净的方法是通过界面。

First create a interface class. 首先创建一个接口类。
AddTimes.java AddTimes.java

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

Now, implement your interface in your class. 现在,在您的班级中实现您的界面。

public class MainActivity extends AppCompatActivity implements AddTimes{

Use your class TextView variable "textTime" 使用您的类TextView变量“textTime”

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

start your class 开始上课

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

At the CounterClass, add constructor. 在CounterClass中,添加构造函数。

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

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

Finally. 最后。 At your main activity. 在你的主要活动。 Implement AddTimes method. 实现AddTimes方法。

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

Copy the code as it is to you MainActivity.java file. 将代码原样复制到MainActivity.java文件中。
Hope this might work. 希望这可行。

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

You can simply use the count down timer: 您只需使用倒计时器:

  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.

相关问题 如何在Android Studio上将字符串从SecondActivity传递给MainActivity? - How do I pass string from SecondActivity to MainActivity on Android Studio? SecondActivity没有收到值-与意图有关 - SecondActivity is not receiving the value - Intent related 如何将MainActivity中的值添加到SecondActivity中的列表中 - How to add values from MainActivity to list in SecondActivity 将ArrayList从SecondActivity传递到Android MainActivity - Passing ArrayList from SecondActivity to MainActivity Android 按下 MainActivity 中的按钮后,SecondActivity 中的 TextView 变得可见? - TextView from SecondActivity become visible after a button from MainActivity is pressed? 如何从SecondActivity调用MainActivity中的非静态方法? - How to call a non-static method in MainActivity from SecondActivity? 如何从secondActivity获取开关按钮的值? - how to get value of switch button from secondActivity? 目的问​​题:如何使用另一个“适配器”类中的MainActivity将数据传递给SecondActivity - Intent problem: how to use MainActivity from another class “Adapter” to pass data to SecondActivity 如何从 Mainactivity 获取一个字符串值到 cardview 适配器? - How to get one string value from the Mainactivity to a cardview Adapter? 如何比较MainActivity的EditText1和SecondActivity的EditText2(SharedPreferences)? - How to compare EditText1 of MainActivity with EditText2 of SecondActivity (SharedPreferences)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM