Android:从计时器线程访问 UI 元素 [英] Android: Accessing UI Element from timer thread

查看:25
本文介绍了Android:从计时器线程访问 UI 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public Button stb;
static int cnt=0;
public ArrayList<RadioButton> Butgrp1 = new ArrayList<RadioButton>();
Timer myt; 
TimerTask t;
stb.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

myt.mschedule(new TimerTask() {

    @Override
    public void run() {

        // TODO Auto-generated method stub


        System.out.println("Entering run");
        Handler h=new Handler();

        h.post(new Runnable() {

            public void run() {

                // TODO Auto-generated method stub
                runOnUiThread(new Runnable() {

                    public void run() {
                        // TODO Auto-generated method stub
                        Butgrp1.get(cnt).setChecked(true);
                        cnt=cnt+1;
                        if(cnt>4)
                            cnt=0;
                        if(cnt>0)
                        //  Butgrp1.get(cnt-1).setChecked(false);
                        System.out.println(cnt);
                    }
                });


            }
        });

        //rg.getChildAt(cnt).setPressed(true);

    }
},1000,2000);

我需要访问 ui 上的一组单选按钮并将其设置为定期检查,但我不断收到不同的错误,我意识到我必须使用处理程序,但它仍然无法正常工作......任何人都可以请告诉我我哪里出错了......我是一个新手,正在尝试一些东西以更好地理解工作......请帮助......

I need to access a group of radio buttons on the ui and set it as checked at regular intervals, but i keep getting different errors, i realized i must use a handler, but its still not working...can anyone please tell me where i am going wrong....am a newbie and am trying out stuff to understand the working better...please help...

推荐答案

您必须在 UI 线程中创建 Handler,即在您的 onCreate活动.

You have to create the Handler in the UI Thread, i.e. in onCreate of your Activity.

因为您在后台线程的 run 方法中创建它,处理程序将在同一个后台线程中执行您的代码.

Because you create it in the run method of a background thread, the handler will execute your code in that very same background thread.

你也可以直接初始化你的Handler:

You could also initialize your Handler directly:

public class MyActivity extends Activity{

    private Handler handler = new Handler();

    //more code
}

然后不要使用runOnUIThread:

 handler.post(new Runnable() {
           public void run() {
                    // TODO Auto-generated method stub
                    Butgrp1.get(cnt).setChecked(true);
                    cnt=cnt+1;
                    if(cnt>4)
                        cnt=0;
                    if(cnt>0)
                    //  Butgrp1.get(cnt-1).setChecked(false);
                    System.out.println(cnt);
                }
            });

好的,试试这个清理过的代码.由于您没有发布完整的活动,因此无法立即使用:

Ok try this cleaned up code. Because you did not post your full Activity this won't work out of the box:

public class TestActivity extends Activity {

    private Button button;
    static int cnt=0;
    public ArrayList<RadioButton> buttonArray = new ArrayList<RadioButton>();
    private Timer timer = new Timer(); 

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                timer.schedule(new MyTimerTask(), 1000,2000);
            }
        });
    }


    private void doButtonStuff(){
        buttonArray.get(cnt).setChecked(true);
        cnt=cnt+1;
        if(cnt>4){
            cnt=0;
        }
        if(cnt>0){
            //  Butgrp1.get(cnt-1).setChecked(false);
            System.out.println(cnt);
        }
    }

    private class MyTimerTask extends TimerTask{

        @Override
        public void run() {        
            runOnUiThread(new Runnable() {              
                @Override
                public void run() {
                    doButtonStuff();
                }
            });
        }       
    }
}

这篇关于Android:从计时器线程访问 UI 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆