我希望onClick方法不仅可以为新页面创建新活动,还可以触发循环结束,这样如果有人点击启动画面的背景,新屏幕就不会重新加载循环停止。
这是我的代码,
package clouds.clouds;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread logotimer = new Thread() {
@Override
public void run(){
try{
int logotimer = 0;
while(logotimer <5000) {
sleep(100);
logotimer = logotimer +100;
}
startActivity(new Intent("clouds.clouds.SPLASH"));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logotimer.start();
}
public void onClickz(View v){}
public void speed2 (View v){
startActivity(new Intent("clouds.clouds.BUTTONZ"));
}
}
有什么建议吗?
答案 0 :(得分:3)
向您的班级添加volatile
布尔变量(称之为cancelled
)。单击按钮时将其设置为true
,并检查cancelled == false
条件下的while
。
public class splash extends Activity {
volatile bool cancelled = false;
...
protected void onCancel(...)
{
cancelled = true;
...
while(!cancelled && logotimer <5000) {
...
答案 1 :(得分:3)
在logotimer.interrupt()
方法中致电onClick()
。这应该导致你的线程中的InterruptedException
你应该通过什么都不做(或者当你打断你的线程时你想做的其他事情)来处理它。
答案 2 :(得分:0)
boolean exit = false;
int logotimer = 0;
while(logotimer <5000 && exit != false) {
sleep(100);
logotimer = logotimer +100;
// value = ???
if(logotimer == value) {
exit = true;
}
}
答案 3 :(得分:0)
package clouds.clouds;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class splash extends Activity {
Thread logotimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
logotimer = new Thread();
Thread logotimer = new Thread() {
@Override
public void run(){
try{
int logotimer = 0;
while(logotimer <5000) {
sleep(100);
logotimer = logotimer +100;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
startActivity(new Intent("clouds.clouds.SPLASH"));
}
};
logotimer.start();
}
public void onClickz (View v){}
public void speed2 (View v){
logotimer.interrupt();
startActivity(new Intent("clouds.clouds.BUTTONZ"));
}
}