Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中为一组按钮执行一个又一个runnable?_Android_Loops_Button_Handler_Runnable - Fatal编程技术网

如何在Android中为一组按钮执行一个又一个runnable?

如何在Android中为一组按钮执行一个又一个runnable?,android,loops,button,handler,runnable,Android,Loops,Button,Handler,Runnable,我对安卓相当陌生。我的目标是让这4个按钮中的每一个都一个接一个地闪烁红色(并返回默认值)。 按钮0闪烁一秒钟,然后切换回默认颜色。 按钮1闪烁一秒钟,然后切换回默认颜色。 等直到整个序列完成。 因此,我为处理程序创建了一个循环,并为每个按钮创建了4个不同的可运行项(无法找到一种机制来解决问题,只为所有按钮创建一个可运行项)。我的代码做了一些超出我预期的事情。它将红色按钮0亮起2秒(大约),按钮2亮起1秒,button1和button3根本不会闪烁,但它们都会经历一个循环周期,因为它们都变成了bt

我对安卓相当陌生。我的目标是让这4个按钮中的每一个都一个接一个地闪烁红色(并返回默认值)。 按钮0闪烁一秒钟,然后切换回默认颜色。 按钮1闪烁一秒钟,然后切换回默认颜色。 等直到整个序列完成。 因此,我为处理程序创建了一个循环,并为每个按钮创建了4个不同的可运行项(无法找到一种机制来解决问题,只为所有按钮创建一个可运行项)。我的代码做了一些超出我预期的事情。它将红色按钮0亮起2秒(大约),按钮2亮起1秒,button1和button3根本不会闪烁,但它们都会经历一个循环周期,因为它们都变成了btn_default_small样式。 此外,btn_default_small并不是默认按钮的样子

public class MainActivity extends ActionBarActivity {

    Button button0;
    Button button1;
    Button button2;
    Button button3;
    private Handler handler;
    int count=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button0 = (Button) findViewById(R.id.button0);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);




        handler = new Handler();
        for(int x=0;x<=3;x++){
            switch(x){
            case 0:
                handler.postDelayed(runnableButton0, 500);
                break;
            case 1:
                handler.postDelayed(runnableButton1, 500);
                break;  
            case 2:
                handler.postDelayed(runnableButton2, 500);
                break;
            case 3:
                handler.postDelayed(runnableButton3, 500);
                break;
            }
        };

    }

    private void LightButton(Button b, Runnable r) {

        if(count==0){
            b.getBackground().setColorFilter(new LightingColorFilter(0xff0000, 0xff0000));//Change button color to red
            count++;
            handler.postDelayed(r, 700);
        }
        else{
            b.setBackgroundResource(android.R.drawable.btn_default_small);//Change button color to default
            count=0;
        }
    }

    Runnable runnableButton0= new Runnable() {
        @Override
        public void run() {
            LightButton(button0, runnableButton0);
        }
    };  

    Runnable runnableButton1= new Runnable() {
        @Override
        public void run() {
            LightButton(button1, runnableButton1);
        }
    };  
    Runnable runnableButton2= new Runnable() {
        @Override
        public void run() {
            LightButton(button2, runnableButton2);
        }
    };  
    Runnable runnableButton3= new Runnable() {
        @Override
        public void run() {
            LightButton(button3, runnableButton3);
        }
    };  
公共类MainActivity扩展了ActionBarActivity{
按钮0;
按钮1;
按钮2;
按钮3;
私人经办人;
整数计数=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0=(Button)findViewById(R.id.button0);
button1=(按钮)findViewById(R.id.button1);
button2=(按钮)findViewById(R.id.button2);
button3=(按钮)findViewById(R.id.button3);
handler=新的handler();
对于(int x=0;x
我做错了什么,或者我对handler和
可运行的操作周期

首先,您同时发布所有这些Runnable,并且在它们之间共享count变量(您不应该这样做),这将使其中一些不会更改背景

private Buttons[] btns =  /* an array holding all your 4 buttons*/
private int count = 0;
Runnable runMe = new Runnable() {
    @Override
    public void run() {
        if (count == btns.length) {
            // we iterated over the entire btns array so abort
            btns[count - 1].getBackground.clearColorFilter();
            return;
        }
        if (count >= 1) {
             // change the previous button back to normal only if
             // we are at the second button
             btns[count - 1].getBackground.clearColorFilter();
        } 
        // change the background
        btns[count].getBackground().setColorFilter(new LightingColorFilter(0xff0000, 0xff0000));
        // increase count to iterate over the btns array
        count++;
        // schedule the next step
        handler.postDelayed(this, 1000);
    }
};  

// in onCreate(),start the flashing with a 1 second delay
handler.postDelayed(runMe, 1000);
上述代码应在初始延迟1秒后,将第一个按钮的背景更改为颜色,然后在1秒后将第一个按钮的背景更改为正常,将第二个按钮的背景更改为颜色(依此类推)

我做错了什么,或者我对handler和 可运行的操作周期

首先,您同时发布所有这些Runnable,并且在它们之间共享count变量(您不应该这样做),这将使其中一些不会更改背景

private Buttons[] btns =  /* an array holding all your 4 buttons*/
private int count = 0;
Runnable runMe = new Runnable() {
    @Override
    public void run() {
        if (count == btns.length) {
            // we iterated over the entire btns array so abort
            btns[count - 1].getBackground.clearColorFilter();
            return;
        }
        if (count >= 1) {
             // change the previous button back to normal only if
             // we are at the second button
             btns[count - 1].getBackground.clearColorFilter();
        } 
        // change the background
        btns[count].getBackground().setColorFilter(new LightingColorFilter(0xff0000, 0xff0000));
        // increase count to iterate over the btns array
        count++;
        // schedule the next step
        handler.postDelayed(this, 1000);
    }
};  

// in onCreate(),start the flashing with a 1 second delay
handler.postDelayed(runMe, 1000);
上述代码应在初始延迟1秒后,将第一个按钮的背景更改为颜色,然后在1秒后将第一个按钮的背景更改为正常,将第二个按钮的背景更改为颜色(依此类推)

我做错了什么,或者我对handler和 可运行的操作周期

首先,您同时发布所有这些Runnable,并且在它们之间共享count变量(您不应该这样做),这将使其中一些不会更改背景

private Buttons[] btns =  /* an array holding all your 4 buttons*/
private int count = 0;
Runnable runMe = new Runnable() {
    @Override
    public void run() {
        if (count == btns.length) {
            // we iterated over the entire btns array so abort
            btns[count - 1].getBackground.clearColorFilter();
            return;
        }
        if (count >= 1) {
             // change the previous button back to normal only if
             // we are at the second button
             btns[count - 1].getBackground.clearColorFilter();
        } 
        // change the background
        btns[count].getBackground().setColorFilter(new LightingColorFilter(0xff0000, 0xff0000));
        // increase count to iterate over the btns array
        count++;
        // schedule the next step
        handler.postDelayed(this, 1000);
    }
};  

// in onCreate(),start the flashing with a 1 second delay
handler.postDelayed(runMe, 1000);
上述代码应在初始延迟1秒后,将第一个按钮的背景更改为颜色,然后在1秒后将第一个按钮的背景更改为正常,将第二个按钮的背景更改为颜色(依此类推)

我做错了什么,或者我对handler和 可运行的操作周期

首先,您同时发布所有这些Runnable,并且在它们之间共享count变量(您不应该这样做),这将使其中一些不会更改背景

private Buttons[] btns =  /* an array holding all your 4 buttons*/
private int count = 0;
Runnable runMe = new Runnable() {
    @Override
    public void run() {
        if (count == btns.length) {
            // we iterated over the entire btns array so abort
            btns[count - 1].getBackground.clearColorFilter();
            return;
        }
        if (count >= 1) {
             // change the previous button back to normal only if
             // we are at the second button
             btns[count - 1].getBackground.clearColorFilter();
        } 
        // change the background
        btns[count].getBackground().setColorFilter(new LightingColorFilter(0xff0000, 0xff0000));
        // increase count to iterate over the btns array
        count++;
        // schedule the next step
        handler.postDelayed(this, 1000);
    }
};  

// in onCreate(),start the flashing with a 1 second delay
handler.postDelayed(runMe, 1000);

上面的代码应该在1秒钟的初始延迟后,将第一个按钮的背景更改为颜色,然后在1秒钟后将第一个按钮的背景更改为正常,将第二个按钮的背景更改为颜色(依此类推)。

非常感谢。效果非常好,我只需要添加Handler=new Handler();并将getBackground更改为getBackground()。非常感谢。非常好用,我只需要添加Handler Handler=new Handler();将getBackground更改为getBackground()。非常感谢。非常好用,我只需要添加Handler Handler=new Handler();将getBackground更改为getBackground()。非常感谢。工作非常完美,我只需要添加Handler=new Handler();并将getBackground更改为getBackground()。