我想要一个Android应用程序,只要我按下按钮,背景就会闪烁。这有效,但是当我释放按钮时,背景不会停止闪烁。 我认为原因是我使用的是Thread。我不确切知道如何阻止它(我第一次使用Threads工作)。我以为我可以在按钮“threadname.stop()”的Action_Up事件中写,但Android工作室无法解析名称。 那么:我如何停止线程以便执行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) {
//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;
}
});
}
}
答案 0 :(得分:0)
添加字段needToRun默认为true。继续while循环,同时: !中断()及;&安培; needToRun。检测TOUCH_UP时将needToRun设置为false。 这是一种在java中停止线程的推荐方法。
答案 1 :(得分:0)
设置布尔字段
private boolean runningThread;
然后将run
方法更改为:
public void run(){
runningThread = true;
while( runningThread ){
// ...
并在发布时:
case MotionEvent.ACTION_UP:{
runningThread = false;
// ...
答案 2 :(得分:0)
从案例MotionEvent.ACTION_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;
}
});
}
}
答案 3 :(得分: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()来停止线程。