2017-06-18 116 views
-2

我想要一個android應用程序,只要按下按鈕,背景就會閃爍。這有效,但是當我釋放按鈕時,背景不會停止閃爍。 我認爲原因是我正在使用線程。我不知道如何阻止它(我正在第一次使用線程)。我想我可以在Action_Up事件中編寫按鈕「threadname.stop()」,但Android studio無法解析名稱。 所以:我怎樣才能停止線程,使Action_Up案例得到執行? 非常感謝!一旦按鈕不再被按下,我怎樣才能停止線程?

我的MainActivity代碼:

import android.graphics.Color; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RelativeLayout; 

public class MainActivity extends AppCompatActivity { 

    Button flash; 
    RelativeLayout layout; 
    boolean backgroundisblack; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     backgroundisblack = true; 
     layout = (RelativeLayout)findViewById(R.id.layout); 
     flash = (Button)findViewById(R.id.flash); 

     //Button 
     flash.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 


       //button is pressed 
       switch (event.getAction()){ 
        case MotionEvent.ACTION_DOWN:{ 

         //thread 
         Thread t = new Thread(){ 
          @Override 
          public void run(){ 
           while(!interrupted()){ 
            try { 
             Thread.sleep(1000); 

             runOnUiThread(new Runnable(){ 
              @Override 
              public void run(){ 
               //action 
               if(backgroundisblack){ 
                layout.setBackgroundColor(Color.WHITE); 
                backgroundisblack = false; 
               }else if (!backgroundisblack){ 
                layout.setBackgroundColor(Color.BLACK); 
                backgroundisblack = true; 
               } 



              } 
             }); 
            }catch (InterruptedException e){ 
             e.printStackTrace(); 
            } 
           } 
          } 
         }; 
         t.start(); 


         break; 
        } 

        //button is released 
        case MotionEvent.ACTION_UP:{ 
         layout.setBackgroundColor(Color.BLACK); 

         break; 
        } 
       } 
       return false; 
      } 
     }); 


    } 
} 
+1

可能的重複[你如何殺死Java中的線程?](https://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java) – user1643723

回答

0

組布爾場

private boolean runningThread; 

然後更改run方法:

public void run(){ 
    runningThread = true; 
    while(runningThread){ 
    // ... 

和釋放:

case MotionEvent.ACTION_UP:{ 
    runningThread = false; 
    // ... 
+0

謝謝你,非常友善。我認爲這會很複雜。 – user7940193

0

添加一個字段needToRun默認爲true。繼續while循環while: !interrupted()& & needToRun。檢測到TOUCH_UP時將needToRun設置爲false。 這是一個推薦的方法來停止java中的線程。

+0

我用了其他方法,但也謝謝你! – user7940193

0
  • 讓你的線程t從案例MotionEvent訪問。行動_ UP 做這個定義在全球範圍內線程t

  • 加t.interrupt()來區分MotionEvent.ACTION_UP

它看起來像

import android.graphics.Color; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RelativeLayout; 

public class MainActivity extends AppCompatActivity { 

Button flash; 
RelativeLayout layout; 
boolean backgroundisblack; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    backgroundisblack = true; 
    layout = (RelativeLayout)findViewById(R.id.layout); 
    flash = (Button)findViewById(R.id.flash); 
    //Button 
    flash.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      Thread t; 
      //button is pressed 
      switch (event.getAction()){ 
       case MotionEvent.ACTION_DOWN:{//thread 
        t = new Thread(){ 
         @Override 
         public void run(){ 
          while(!interrupted()){ 
           try { 
            Thread.sleep(1000); 
            runOnUiThread(new Runnable(){ 
             @Override 
             public void run(){ 
              //action 
              if(backgroundisblack){ 
               layout.setBackgroundColor(Color.WHITE); 
               backgroundisblack = false; 
              }else if (!backgroundisblack){ 
               layout.setBackgroundColor(Color.BLACK); 
               backgroundisblack = true; 
              } 



             } 
            }); 
           }catch (InterruptedException e){ 
            e.printStackTrace(); 
           } 
          } 
         } 
        }; 
        t.start(); 


        break; 
       } 

       //button is released 
       case MotionEvent.ACTION_UP:{ 
        layout.setBackgroundColor(Color.BLACK); 
        t.interrupt() 
        break; 
       } 
      } 
      return false; 
     } 
    }); 

} 
} 
0

,你可以把一個更使用按鈕時按下按鈕時以及按下按鈕時

// this goes somewhere in your class: 
    long lastDown; 
    long lastDuration; 

    ... 

    // this goes wherever you setup your button listener: 
    button.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if(event.getAction() == MotionEvent.ACTION_DOWN) { 
      lastDown = System.currentTimeMillis(); 
     } else if (event.getAction() == MotionEvent.ACTION_UP) { 
      lastDuration = System.currentTimeMillis() - lastDown; 
     } 
    } 
    }); 

,從而可以使用thread.interrupt()來停止線程。

相關問題