闪烁屏幕活动Android



我正在尝试测试一些东西,但我所需要的只是一个活动,在黑白屏幕之间以1秒的间隔闪烁。我看过postDelayed,但我仍然不确定我将如何实现它。这是我现在看到的,但是它不会"闪烁"

MainActivity:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
public class Blink extends Activity {
    int i = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blink);
        while (i == 0) {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    changeColor();
                }
            }, 1000);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void changeColor() {
        if (findViewById(R.id.mylayout).getBackground().equals("#ffffff")) {
            findViewById(R.id.mylayout).setBackgroundResource(Color.BLACK);
        } else {
            findViewById(R.id.mylayout).setBackgroundResource(Color.WHITE);
        }
    }
}

更新代码:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
public class Blink extends Activity {
    int i = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blink);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                changeColor();
            }
        }, 1000);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void changeColor() {
        if (i == 0) {
            findViewById(R.id.mylayout).setBackgroundColor(Color.WHITE);
            i++;
        } else if (i == 1) {
            findViewById(R.id.mylayout).setBackgroundColor(Color.BLACK);
            i--;
        }
    }
}
更新2:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
public class Blink extends Activity {
    int i = 0;
    Boolean bool = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blink);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                changeColor();
            }
        }, 1000);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void changeColor() {
        if (bool) {
            findViewById(R.id.mylayout).setBackgroundColor(Color.WHITE);
            bool = false;
        } else {
            findViewById(R.id.mylayout).setBackgroundColor(Color.BLACK);
            bool = true;
        }
    }
}

我缩小了我应该在这里问的确切问题:为什么这个postDelayed不能永远运行?

要修复这段代码,我所要做的就是在run中添加handler.postDelayed(this, 1000);

最新更新