0

我希望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"));
    }



}

有什么建议么?

4

4 回答 4

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) {
...
于 2012-07-15T01:16:16.133 回答
3

调用logotimer.interrupt()你的onClick()方法。这应该会导致InterruptedException你的线程中出现一个你应该不做任何事情来处理的问题(或者当你中断你的线程时你想做的任何其他事情)

于 2012-07-15T01:46:01.863 回答
0
boolean exit = false;
int logotimer = 0;
while(logotimer <5000 && exit != false) {
  sleep(100);
  logotimer = logotimer +100;
  // value = ???    
  if(logotimer == value) {
      exit = true;
  }
}
于 2012-07-15T02:23:17.647 回答
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"));
}






}
于 2012-07-15T22:40:20.673 回答