| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3363 人关注过本帖
标题:线程实现数字递减问题!!
只看楼主 加入收藏
绝爱小朱
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
快速回复:线程实现数字递减问题!!
数据加载中...
 
   



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

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