Android 如何从线程内部更改relativeLayout背景色

Android 如何从线程内部更改relativeLayout背景色,android,multithreading,Android,Multithreading,我有一个按钮btn开关,我想让闪光灯闪烁,背景颜色持续变化,只要我按住按钮,并在松开手指时停止这样做,背景颜色是白色和黑色,闪光灯正常工作,但背景只会改变一次黑色和黑色永远不会回到白色 代码如下: public class MainActivity extends AppCompatActivity { public static RelativeLayout relativeLayout; public Button btnSwitch; btnSwitch.se

我有一个按钮
btn开关
,我想让闪光灯闪烁,背景颜色持续变化,只要我按住按钮,并在松开手指时停止这样做,背景颜色是白色和黑色,闪光灯正常工作,但背景只会改变一次黑色和黑色永远不会回到白色

代码如下:

public class MainActivity extends AppCompatActivity {
    public static RelativeLayout relativeLayout;
    public Button btnSwitch;

      btnSwitch.setOnTouchListener(new View.OnTouchListener() {
            private Handler handler;
            @Override
            public boolean onTouch(View v,  MotionEvent event) {

                switch (event.getAction()){

                    case MotionEvent.ACTION_DOWN:
                        if (handler != null) return true;
                        handler = new Handler();
                        handler.postDelayed(thread, 14);
                        break;

                    case MotionEvent.ACTION_UP:
                       if (handler == null) return true;
                        handler.removeCallbacks(thread);
                        handler = null;
                        break;
            }
                return false;
            }
           Runnable thread= new Runnable() {
                @Override
                public void run() {
                    synchronized (this) {
                        try {
                            this.wait(7);
                            relativeLayout.setBackgroundColor(Color.BLACK);
                            turnonflash();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        try {
                            this.wait(7);
                            relativeLayout.setBackgroundColor(Color.WHITE);
                            turnofflash();

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.postDelayed(this, 14);
                    }}
           };
        });
    }
这是XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"

android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/backv">

<Button

    android:id="@+id/btnSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dip"

    android:text="switch"

    android:contentDescription="@null"
    />


您需要调用
relativeLayout.setBackgroundColor(颜色):

 activity.runOnUiThread(new Runnable() {
      @Override
          public void run() {
              relativeLayout.setBackgroundColor(color);
     }
 });

您需要调用
relativeLayout.setBackgroundColor(颜色):

 activity.runOnUiThread(new Runnable() {
      @Override
          public void run() {
              relativeLayout.setBackgroundColor(color);
     }
 });

同样的问题在第二次跑步时背景变黑,然后第一次跑步时阅读永远不会再次运行这只是一个想法-但是试着设置7到1000进行测试-也许你的代码可以工作,但是7ms太短了,人眼无法识别颜色变化?同样的问题在第二次跑步时背景变黑,然后开始阅读first RunNuithread从不再次运行这只是一个想法-但是试着将测试设置为7到1000-也许你的代码可以工作,但是7ms太短,人眼无法识别颜色变化?