Android 的简单提醒

发布于 2024-09-03 11:07:36 字数 2387 浏览 2 评论 0原文

我正在尝试制作一个简单的计时器。

package com.anta40.reminder;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost.TabSpec;

public class Reminder extends Activity{

    public final int TIMER_DELAY = 1000;
    public final int TIMER_ONE_MINUTE = 60000; 
    public final int TIMER_ONE_SECOND = 1000;
    Timer timer;
    TimerTask task;
    TextView tv;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.main);
        timer = new Timer();

        task = new TimerTask() {

            @Override
            public void run() {
                tv = (TextView) findViewById(R.id.textview1);
                tv.setText("BOOM!!!!");
                tv.setVisibility(TextView.VISIBLE);
                try {
                    this.wait(TIMER_DELAY);
                }
                catch (InterruptedException e){

                }
                tv.setVisibility(TextView.INVISIBLE);
            }

        };

        TabHost tabs=(TabHost)findViewById(R.id.tabhost);

        tabs.setup();

        TabSpec spec = tabs.newTabSpec("tag1");

        spec.setContent(R.id.tab1);
        spec.setIndicator("Clock");
        tabs.addTab(spec);

        spec=tabs.newTabSpec("tag2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("Settings");
        tabs.addTab(spec);

        tabs.setCurrentTab(0);

        RadioGroup rgroup = (RadioGroup) findViewById(R.id.rgroup);
        rgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.om){
                    timer.schedule(task, TIMER_DELAY, 3*TIMER_ONE_SECOND);
                }
                else if (checkedId == R.id.twm){
                    timer.schedule(task, TIMER_DELAY, 6*TIMER_ONE_SECOND);
                }
                else if (checkedId == R.id.thm){
                    timer.schedule(task, TIMER_DELAY, 9*TIMER_ONE_SECOND);
                }
            }
        });

    }
}

每次我单击单选按钮时,计时器都应该启动,对吧? 但为什么不开始呢?

I'm trying to make a simple timer.

package com.anta40.reminder;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost.TabSpec;

public class Reminder extends Activity{

    public final int TIMER_DELAY = 1000;
    public final int TIMER_ONE_MINUTE = 60000; 
    public final int TIMER_ONE_SECOND = 1000;
    Timer timer;
    TimerTask task;
    TextView tv;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.main);
        timer = new Timer();

        task = new TimerTask() {

            @Override
            public void run() {
                tv = (TextView) findViewById(R.id.textview1);
                tv.setText("BOOM!!!!");
                tv.setVisibility(TextView.VISIBLE);
                try {
                    this.wait(TIMER_DELAY);
                }
                catch (InterruptedException e){

                }
                tv.setVisibility(TextView.INVISIBLE);
            }

        };

        TabHost tabs=(TabHost)findViewById(R.id.tabhost);

        tabs.setup();

        TabSpec spec = tabs.newTabSpec("tag1");

        spec.setContent(R.id.tab1);
        spec.setIndicator("Clock");
        tabs.addTab(spec);

        spec=tabs.newTabSpec("tag2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("Settings");
        tabs.addTab(spec);

        tabs.setCurrentTab(0);

        RadioGroup rgroup = (RadioGroup) findViewById(R.id.rgroup);
        rgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.om){
                    timer.schedule(task, TIMER_DELAY, 3*TIMER_ONE_SECOND);
                }
                else if (checkedId == R.id.twm){
                    timer.schedule(task, TIMER_DELAY, 6*TIMER_ONE_SECOND);
                }
                else if (checkedId == R.id.thm){
                    timer.schedule(task, TIMER_DELAY, 9*TIMER_ONE_SECOND);
                }
            }
        });

    }
}

Each time I click a radio button, the timer should start, right?
But why it doesn't start?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2024-09-10 11:07:36

首先,您无法从后台线程修改 UI。因此您无法从 TimerTask 修改 UI。

其次,即使你的“BOOM!”逻辑位于主应用程序线程上,您不允许 Android 执行任何操作。 Android 的 UI 是消息和事件驱动的。当您调用 setText()setVisibility() 时,它们只有在您将控制权返回给 Android 后才会生效。因此,在您的情况下,您将看不到任何内容,因为 TextView 在变得可见后立即变得不可见。

第三,如果您确实将代码移至主应用程序线程,请不要阻塞该线程,例如使用 wait() 调用。

First, you cannot modify the UI from a background thread. So you cannot modify the UI from a TimerTask.

Second, even if your "BOOM!" logic were on the main application thread, you are not allowing Android to do anything. Android's UI is message- and event-driven. When you call setText() and setVisibility(), they do not take effect until you return control to Android. Hence, in your case, you would not see anything, since the TextView would become invisible immediately after becoming visible.

Third, if you do move your code to the main application thread, do not block that thread, such as with your wait() call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文