标题:线程实现数字递减问题!!
只看楼主
绝爱小朱
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-3-26
结帖率:33.33%
 问题点数:0 回复次数:1 
线程实现数字递减问题!!
我要在AlertDialog上面的TextView数字每秒递减一,但是每隔几秒减4,可能是线程原因但找到原因,求大神指导。。
下面是我的代码

public class MainActivity extends Activity {
    TextView tv = null;
    int i = 60;
    UIUpdateThread ui = null;

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what < 0) {

                aDialog.dismiss();
                timer.cancel();
            }
        }
    };
    AlertDialog aDialog = null;
    Timer timer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LayoutInflater mInflater = (LayoutInflater) MainActivity.this
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        ui = new UIUpdateThread();
        View view = mInflater.inflate(R.layout.activity_main, null);
        RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.back);
        tv = new TextView(getApplicationContext());
        layout.addView(tv);
        aDialog = new AlertDialog.Builder(MainActivity.this).setMessage("")
                .create();
        aDialog.setTitle("时间倒计时:");
        aDialog.setView(view);
        new Thread(new UIUpdateThread()).start();
        aDialog.show();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                Message msg = handler.obtainMessage();
                msg.what = i;
                msg.obj = ui;
                handler.sendEmptyMessage(msg.what);
                handler.post(ui);
                i = i - 1;
            }
        }, 1000, 1000);
    }

    class UIUpdateThread implements Runnable {
        final Handler mHandler = new Handler();

        final Runnable mUpdateResults = new Runnable() {
            public void run() {

                concreteUpdateUI();
                tv.invalidate();
            }

        };

        public void run() {
            // delay some minutes you desire.

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            mHandler.post(mUpdateResults);
        }

    }

    protected void concreteUpdateUI() {
        // Add concrete movement for UI updates.

        i--;
        tv.setText(String.valueOf(i));

    }
搜索更多相关主题的帖子: public null 
2014-02-20 19:25
绝爱小朱
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-3-26
得分:0 
自己解决了,
2014-02-20 20:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-427468-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.772746 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved