2012-07-19 74 views
0
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)); 
      } 
    } 
+0

TextView tv = new TextView(this); ???? – 2012-07-19 06:33:48

+0

你爲什麼一再重複同樣的問題?這篇文章是http://stackoverflow.com/questions/11305930/i-embedded-a-countdown-timer-in-this-code-but-it-didn的副本 – sunil 2012-07-19 06:52:41

回答

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)); 
     } 
    } 
} 
+0

非常感謝! – 2012-07-19 07:38:15

+0

如果它幫助你,將其標記爲答案 – sunil 2012-07-19 08:08:32