跳过启动画面
Skipping a Splash Screen
我设计了一个带按钮的闪屏。 Java 代码如下。启动画面的布局包含一些带有动画的文本和名为 skipped splash screen 的按钮。当用户按下按钮时,启动画面必须立即停止并打开下一个activity。但是当我打开启动画面并按下跳过按钮时,下一个 activity 打开但是在启动画面必须 运行 结束的持续时间之后,activity 再次打开。如何在用户按下跳过按钮时停止启动画面?
public class Qz1 extends Activity {
TextView a;
TextView b;
TextView c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qz1);
a =(TextView)findViewById(R.id.roundOnea22);
a.startAnimation(AnimationUtils.loadAnimation(Qz1.this, R.anim.anim_slide_in_left));
b =(TextView)findViewById(R.id.roundOneb);
b.startAnimation(AnimationUtils.loadAnimation(Qz1.this, R.anim.anim_slide_in_right));
c =(TextView)findViewById(R.id.roundme);
c.startAnimation(AnimationUtils.loadAnimation(Qz1.this, R.anim.anim_slide_in_left));
Thread thread = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
try{
sleep(3200);
startActivity(new Intent(getApplicationContext(), Qone.class));
} catch (InterruptedException e){
e.printStackTrace();
}
}
};
thread.start();
}
public void round1(View v){
Intent i = new Intent(Qz1.this, Qone.class);
startActivity(i);
}
}
最佳做法是将异步任务用于 wait/sleep 场景,例如初始屏幕,但要求可能有所不同。
无论如何,这是我调用启动画面的方式:
首先创建 AsyncTask
。
private class SplashTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
finish();
Intent intent = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(intent);
}
}
}
然后在任何你想要的地方调用它:点击按钮、开始或创建:
new SplashTask().execute();
不应该使用 sleep(2000)
使用动画监听器(http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html)
当 onAnimationEnd 触发时调用 startActivity。
在启动画面中试试这个 activity
Button button = (Button)findViewById(R.id.buttonLayout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(this,TargetActivity.class));
finish();
}
});
假设您希望将第一个 activity 保留在后台,但不希望线程在完成休眠后立即重新打开第二个 activity。
为了实现这一点,您可以将 "thread" 设为自定义 Thread
class 的全局变量。您可以将其定义为 activity:
的内部 class
MyThread thread;
和 class 定义:
private class MyThread extends Thread
{
public boolean bRun = true;
@Override
public void run()
{
try
{
sleep(3200);
if (bRun)
{
startActivity(new Intent(getApplicationContext(), Activity2.class));
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
在onCreate()
中,你写
thread = new MyThread();
thread.start();
然后你可以像这样改变你的"onClick"方法:
public void round1(View v){
if (thread != null && thread.isAlive())
{
thread.bRun = false;
}
Intent i = new Intent(Qz1.this, Qone.class);
startActivity(i);
}
这将阻止线程启动第二个 activity,如果它是通过单击按钮启动的。
我认为这里的最佳做法是使用处理程序。
你可以这样做:
public class Test extends AppCompatActivity{
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Settign the splashscreen with the button i suppose
setContentView(R.id.splashcreen);
}
@Override
protected void onStart() {
super.onStart();
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startNextActivity();
}
}, 2000);
}
public void startNextActivity(){
startActivity(new Intent(getApplicationContext(), Qone.class));
}
public void skipSplashScreen(){
if (handler != null)
handler.removeCallbacksAndMessages(null);
startNextActivity();
}
@Override
protected void onStop() {
super.onStop();
// clear handler on stop
if (handler != null)
handler.removeCallbacksAndMessages(null);
}
}
CAll skipSplashScreen() 当用户按下按钮时,处理程序将停止,因此计时器停止,您可以通过调用方法 startNextActivity() 手动转到下一个 activity。
我设计了一个带按钮的闪屏。 Java 代码如下。启动画面的布局包含一些带有动画的文本和名为 skipped splash screen 的按钮。当用户按下按钮时,启动画面必须立即停止并打开下一个activity。但是当我打开启动画面并按下跳过按钮时,下一个 activity 打开但是在启动画面必须 运行 结束的持续时间之后,activity 再次打开。如何在用户按下跳过按钮时停止启动画面?
public class Qz1 extends Activity {
TextView a;
TextView b;
TextView c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qz1);
a =(TextView)findViewById(R.id.roundOnea22);
a.startAnimation(AnimationUtils.loadAnimation(Qz1.this, R.anim.anim_slide_in_left));
b =(TextView)findViewById(R.id.roundOneb);
b.startAnimation(AnimationUtils.loadAnimation(Qz1.this, R.anim.anim_slide_in_right));
c =(TextView)findViewById(R.id.roundme);
c.startAnimation(AnimationUtils.loadAnimation(Qz1.this, R.anim.anim_slide_in_left));
Thread thread = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
try{
sleep(3200);
startActivity(new Intent(getApplicationContext(), Qone.class));
} catch (InterruptedException e){
e.printStackTrace();
}
}
};
thread.start();
}
public void round1(View v){
Intent i = new Intent(Qz1.this, Qone.class);
startActivity(i);
}
}
最佳做法是将异步任务用于 wait/sleep 场景,例如初始屏幕,但要求可能有所不同。
无论如何,这是我调用启动画面的方式:
首先创建 AsyncTask
。
private class SplashTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
finish();
Intent intent = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(intent);
}
}
}
然后在任何你想要的地方调用它:点击按钮、开始或创建:
new SplashTask().execute();
不应该使用 sleep(2000)
使用动画监听器(http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html)
当 onAnimationEnd 触发时调用 startActivity。
在启动画面中试试这个 activity
Button button = (Button)findViewById(R.id.buttonLayout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(this,TargetActivity.class));
finish();
}
});
假设您希望将第一个 activity 保留在后台,但不希望线程在完成休眠后立即重新打开第二个 activity。
为了实现这一点,您可以将 "thread" 设为自定义 Thread
class 的全局变量。您可以将其定义为 activity:
MyThread thread;
和 class 定义:
private class MyThread extends Thread
{
public boolean bRun = true;
@Override
public void run()
{
try
{
sleep(3200);
if (bRun)
{
startActivity(new Intent(getApplicationContext(), Activity2.class));
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
在onCreate()
中,你写
thread = new MyThread();
thread.start();
然后你可以像这样改变你的"onClick"方法:
public void round1(View v){
if (thread != null && thread.isAlive())
{
thread.bRun = false;
}
Intent i = new Intent(Qz1.this, Qone.class);
startActivity(i);
}
这将阻止线程启动第二个 activity,如果它是通过单击按钮启动的。
我认为这里的最佳做法是使用处理程序。
你可以这样做:
public class Test extends AppCompatActivity{
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Settign the splashscreen with the button i suppose
setContentView(R.id.splashcreen);
}
@Override
protected void onStart() {
super.onStart();
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startNextActivity();
}
}, 2000);
}
public void startNextActivity(){
startActivity(new Intent(getApplicationContext(), Qone.class));
}
public void skipSplashScreen(){
if (handler != null)
handler.removeCallbacksAndMessages(null);
startNextActivity();
}
@Override
protected void onStop() {
super.onStop();
// clear handler on stop
if (handler != null)
handler.removeCallbacksAndMessages(null);
}
}
CAll skipSplashScreen() 当用户按下按钮时,处理程序将停止,因此计时器停止,您可以通过调用方法 startNextActivity() 手动转到下一个 activity。