1

我正在使用倒数计时器倒计时这是我的代码:

public class countdownnow extends Activity{

    TextView tv;
    private CDwnTimer countDownTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState); 

        setContentView(R.layout.countdown_disp);
        tv = (TextView)findViewById(R.id.textView1);
    countDownTimer = new MalibuCountDownTimer(30000, 1050);
        countDownTimer.start();
}


// CountDownTimer class
    public class CDwnTimer extends CountDownTimer {
        public CDwnTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            tv.setText("00");             
        }

        @Override
        public void onTick(long millisUntilFinished) {

            int convert = (int)  millisUntilFinished/1000;
            String testString = String.format("%02d",convert); 

    CharSequence cs = new String(testString);                   
    tv.setText(cs);                 
        }
    }
}

最初这工作正常但是当我离开这个活动然后去另一个活动然后回来时,文本视图不再倒计时了吗?TextView tv 继续运行,但它没有更新。Activity 是否被释放并初始化 Textview 的相同 id?任何提示都会很棒。谢谢。

4

1 回答 1

0

你是怎么离开活动的?

我模拟了您的程序,当我将 MainActivity 留给 OtherActivity 并返回时,textView 继续倒计时。代码如下:

public class MainActivity extends Activity {

private CDwnTimer countDownTimer;
private Button btn;
private TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv = (TextView)findViewById(R.id.tv);
    btn = (Button) findViewById(R.id.btnEnter);

    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // call sendMessage method
            Intent startActivityIntent =new Intent(MainActivity.this, OtherActivity.class);
            MainActivity.this.startActivity(startActivityIntent);
        }
    });

    countDownTimer = new CDwnTimer(30000, 1050);
    countDownTimer.start();
}



// CountDownTimer class
    public class CDwnTimer extends CountDownTimer {
        public CDwnTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            tv.setText("00");             
        }

        @Override
        public void onTick(long millisUntilFinished) {
            int convert = (int)  millisUntilFinished/1000;
            String testString = String.format("%02d",convert); 
            CharSequence cs = new String(testString);  
            tv.setText(cs);
        }
    }
}

MainActivity 布局的 main.xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"       
        android:text="TextView" 
        android:layout_weight="6"/>
    <Button
        android:id="@+id/btnEnter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="282dp"
        android:layout_marginLeft="21dp"
        android:text="Button" 
        android:layout_weight="2"/>
</LinearLayout>
于 2013-01-28T10:51:41.230 回答