CountDownTimer将不会显示

时间:2012-07-19 06:29:41

标签: android android-layout countdowntimer

package com.android.tapme;

import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.*;
import android.os.CountDownTimer;

public class TapMe extends Activity {

private int countValue=0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tap_me);
    checkTapValue();
}
private void checkTapValue()
{
    Button tapButton=(Button)findViewById(R.id.tapButton);
    tapButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            countValue++;
            TextView textView;
            textView = (TextView) findViewById(R.id.textView1);
            textView.setTextSize(40);
            textView.setText(Integer.toString(countValue)); 
        }
    });     

}
@Override
protected void onResume()
{
    super.onResume();
    checkTapValue();
}
}
     class MyCount extends CountDownTimer{
TextView tv;
   public MyCount(long millisInFuture, long countDownInterval) {
        super(60000, 1000);
        }
        @Override
        public void onFinish(){}
        @Override
        public void onTick(long millisUntilFinished) {
        tv.setText((int) (millisUntilFinished/1000));
        }   
 }

这是我的.java文件。倒数计时器应显示60秒并每秒倒计时。事情是一切正常,但倒数计时器。它不显示。 有谁可以指出我做错了什么,拜托?

此外,这是布局文件。

<RelativeLayout 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:background="#FFFFFF" >

<Button
    android:id="@+id/tapButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginTop="186dp"
    android:padding="15dp"
    android:text="@string/tap_me"
    android:textSize="32dp" />


<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_alignParentTop="true"
    android:layout_marginTop="85dp"
    android:padding="@dimen/padding_medium"
    android:textColor="#000000"
    tools:context=".TapMe" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:padding="@dimen/padding_medium"
    android:textColor="#000000"
    tools:context=".TapMe" />

 </RelativeLayout>

EDIT @imran 我做到了这一点。没有区别。

class MyCount extends CountDownTimer{
    TextView tv = (TextView) findViewById(R.id.textView2);
       public MyCount(long millisInFuture, long countDownInterval) {
            super(60000, 1000);
            }
            private TextView findViewById(int textview2) {
        // TODO Auto-generated method stub
        return null;
    }
            @Override
            public void onFinish(){}
            @Override
            public void onTick(long millisUntilFinished) {
            tv.setText("" +(int) (millisUntilFinished/1000));
            }   
     }

1 个答案:

答案 0 :(得分:1)

试试这个更新的代码。工作得很好。你根本没有启动倒计时器,也从未在计时器中初始化textview。我在下面的代码中更改了这些错误

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TapMe extends Activity {

    private int countValue = 0;
    private TextView textView;
    private TextView textView2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tap_me);

        textView = (TextView) findViewById(R.id.textView1);
        textView.setTextSize(40);
        textView2 = (TextView) findViewById(R.id.textView2);
        textView2.setTextSize(40);

        checkTapValue();

        MyCount myCount = new MyCount(60000, 1000);
        myCount.start();

    }

    private void checkTapValue() {
        Button tapButton = (Button) findViewById(R.id.tapButton);
        tapButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                countValue++;
                textView.setText(Integer.toString(countValue));

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        checkTapValue();
    }

    class MyCount extends CountDownTimer {

        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
        }

        @Override
        public void onTick(long millisUntilFinished) {
            System.out.println(millisUntilFinished);
            textView2.setText("" + (int) (millisUntilFinished / 1000));
        }
    }
}