2014-01-22 132 views
0

首先,我必須承認,我是一個開發android編程的人,可能無法正確理解事情。臨時加載屏幕

其次,問題: 在我的應用程序中,我創建了一個加載屏幕,並且我已經嘗試限制這個屏幕的時間,所以當時間結束時 - 它會通過意向移動到另一個屏幕。

代碼:

public class MainActivity extends Activity implements OnClickListener { 

ImageView iv; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.loading_screen); 
    iv=(ImageView)findViewById(R.id.imgBtn1); 
    iv.setBackgroundResource(R.anim.loading_i_animation); 
    iv.setOnClickListener(this); 

    } 


public void onClick(final View iv) { 
    // TODO Auto-generated method stub 

    Thread t1=new Thread(new Runnable() { 

     @Override 
     public void run() { 

    AnimationDrawable anim=(AnimationDrawable) iv.getBackground(); 
    anim.start(); 

     } 
    }); 

    t1.start(); 
    try { 
     t1.sleep(2000); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    t1.stop(); 

    //The moving to the other screen 

    Intent st=new Intent(MainActivity.this,Welcome.class); 
    startActivity(st); 


} } 

的意圖本身的工作,還有loading_screen的動畫。但是,當我編寫「t1.stop();」爲了阻止線程 - 它聽到它。

回答

0

我明白了。我忘記使用finish()並繼續我有「最後」的選項。

正確的代碼:

public class MainActivity extends Activity implements OnClickListener { 

ImageView iv; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.loading_screen); 
    iv=(ImageView)findViewById(R.id.imgBtn1); 
    iv.setBackgroundResource(R.anim.loading_i_animation); 
    iv.setOnClickListener(this); 

    } 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public void onClick(final View iv) { 
    // TODO Auto-generated method stub 

    Thread t1=new Thread(new Runnable() { 

     @Override 
     public void run() { 

      AnimationDrawable anim=(AnimationDrawable) iv.getBackground(); 
      anim.start(); 

     } 
    }); 
    t1.start(); 
    try { 
     t1.sleep(1000); 
     finish(); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    finally { 
     Intent st=new Intent(MainActivity.this,Welcome.class); 
     startActivity(st); 
    } 




} 
}