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秒並每秒倒計時。事情是一切正常,但倒數計時器。它不顯示。 任何人都可以指出我做錯了嗎?CountDownTimer將不會顯示
另外,這裏是佈局文件。
<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>
編輯 @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));
}
}
TextView tv = new TextView(this); ???? – 2012-07-19 06:33:48
你爲什麼一再重複同樣的問題?這篇文章是http://stackoverflow.com/questions/11305930/i-embedded-a-countdown-timer-in-this-code-but-it-didn的副本 – sunil 2012-07-19 06:52:41