如何在Android测验中实现计时器?

时间:2013-09-02 06:36:50

标签: java android timer imageview

我正在进行Android测验。我想在Question XML上的ImageView上添加计时器,我想在Play按钮上从CountDownTimer调用Welcome XML。我只是在onClick中设置了Question xml,在ImageView中添加了这样的一行

  

问题XML

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/question"
    android:layout_centerHorizontal="true"
    android:background="@drawable/timer_bttn" 
    android:onClick="onClick"/>
  

欢迎使用xml

<Button 
            android:text="Play" 
            android:id="@+id/playBtn"
            android:layout_width="80dip" 
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content"
            android:paddingTop="5dip" 
            android:paddingBottom="5dip"
            android:textColor="#ffffff"
            android:background="@drawable/start_button" />
  

然后我创建了TimerActivity

    public class TimerActivity extends Activity implements OnClickListener {

private Button startB;
public TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
startB = (Button) this.findViewById(R.id.playBtn);
startB.setOnClickListener((OnClickListener) this);
text = (TextView) findViewById(R.id.textView1);
}

@Override
public void onClick(View v) {
    new CountDownTimer(30000, 1000) { // adjust the milli seconds here

        public void onTick(long millisUntilFinished) {
        text.setText(""+String.format("%d min, %d sec", 
                        TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
        }

        public void onFinish() {
           text.setText("done!");
        }
     }.start();

}   
}

在这段代码中,我刚刚编写了定时器代码来调用Image并在TimerActivity中实现代码。但是当我单击播放按钮时,我的计时器无法启动。我的播放按钮在不同的活动中工作。任何人都可以帮助我如何在不同的XML中调用计时器。

我的logCat: -

!ENTRY org.eclipse.ui 4 0 2013-09-02 17:19:38.741
!MESSAGE Unhandled event loop exception
!STACK 0
java.lang.IllegalArgumentException: Widget has the wrong parent
    at org.eclipse.swt.SWT.error(SWT.java:4342)
    at org.eclipse.swt.SWT.error(SWT.java:4276)
    at org.eclipse.swt.SWT.error(SWT.java:4247)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
    at org.eclipse.swt.widgets.Control.setMenu(Control.java:3462)
    at com.android.ddmuilib.logcat.LogCatPanel$20.handleEvent(LogCatPanel.java:1042)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
    at org.eclipse.swt.widgets.Widget.showMenu(Widget.java:1472)
    at org.eclipse.swt.widgets.Widget.wmContextMenu(Widget.java:1574)
    at org.eclipse.swt.widgets.Control.WM_CONTEXTMENU(Control.java:4673)
    at org.eclipse.swt.widgets.Table.WM_CONTEXTMENU(Table.java:5972)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4532)
    at org.eclipse.swt.widgets.Table.windowProc(Table.java:5913)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:4989)
    at org.eclipse.swt.internal.win32.OS.CallWindowProcW(Native Method)
    at org.eclipse.swt.internal.win32.OS.CallWindowProc(OS.java:2440)
    at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:564)
    at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:430)
    at org.eclipse.swt.widgets.Widget.wmRButtonUp(Widget.java:2395)
    at org.eclipse.swt.widgets.Control.WM_RBUTTONUP(Control.java:5134)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4596)
    at org.eclipse.swt.widgets.Table.windowProc(Table.java:5913)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:4989)
    at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
    at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2546)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3756)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1414)

提前感谢

2 个答案:

答案 0 :(得分:1)

我没看到你的计时器实现有什么问题

但你有这个

 text = (TextView) this.findViewById(R.id.button1);

你有这个

   <ImageView
android:id="@+id/button1"

所以它应该是

 <TextView
android:id="@+id/textView1" 

text = (TextView) findViewById(R.id.textView1);

尝试以下方法。点击按钮计时器启动。

public class MainActivity extends Activity implements OnClickListener {

private Button startB;
public TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB = (Button) this.findViewById(R.id.button1);
startB.setOnClickListener((OnClickListener) this);
text = (TextView) findViewById(R.id.textView1);
}

@Override
public void onClick(View v) {
    new CountDownTimer(30000, 1000) { // adjust the milli seconds here

        public void onTick(long millisUntilFinished) {
        text.setText(""+String.format("%d min, %d sec", 
                        TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
        }

        public void onFinish() {
           text.setText("done!");
        }
     }.start();

}   
}

检查此链接

对于低于1.5的Java版本或对于不完全支持TimeUnit类的系统,可以使用以下等式:

int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours   = (int) ((milliseconds / (1000*60*60)) % 24);

编辑:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="68dp"
        android:text="Button" />

</RelativeLayout>

enter image description here

enter image description here

答案 1 :(得分:0)

您应该尝试使用以下代码。

private void setTimer() {
    counterTimer = new CountDownTimer(finishTime * 1000, 1000) {
        public void onFinish() {
            //code to execute when time finished
        }

        public void onTick(long millisUntilFinished) {
            int seconds = (int) (millisUntilFinished / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;

            if (seconds < 10) {
                txtTimer.setText("" + minutes + ":0" + seconds);
            } else {
                txtTimer.setText("" + minutes + ":" + seconds);
            }
        }
    };
    counterTimer.start();
}

此处examFinishTime是一个很长的值,如

private long finishTime = 5; 

注意:参数以毫秒为单位。

要启动计时器,只需在任何按钮的点击事件中调用setTimer()方法。