【问题标题】:How to move from one activity to another after certain time automatically in C#如何在 C# 中自动在一定时间后从一个活动移动到另一个活动
【发布时间】:2016-05-27 22:53:51
【问题描述】:

我需要从第 1 项活动转到第 2 项活动,睡眠时间为 5 秒,然后再转到第 2 项活动。我的第一个活动包含使用以下代码旋转的图像-

ImageForRotation.StartAnimation(loadedImage)

第二个活动是登录页面。这是我的代码-

base.OnCreate(bundle);
SetContentView(Resource.Layout.SplashScreen);
ImageView ImageForRotation = FindViewById < ImageView > (Resource.Id.imageForRotation);

var loadedImage = AnimationUtils.LoadAnimation(this, Resource.Animation.SplashScreenImageRotation);

ImageForRotation.StartAnimation(loadedImage);
//here the code for 5 second wait time which i don't know
StartActivity(typeof(LoginScreen)); //second activity

【问题讨论】:

    标签: c# android visual-studio android-activity xamarin.android


    【解决方案1】:

    这样,这是你的第一个活动

    protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           NextActivity();
    }
    
    public void NextActivity()
    {   
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
    
                Intent mainIntent = new Intent(MainActivity.this,Main2Activity.class);
                MainActivity.this.startActivity(mainIntent);
                MainActivity.this.finish();
            }
        }, 5000);
    
    }
    
    【解决方案2】:

    我不知道你为什么要等待 5 秒,但是如果你想在动画完成后启动 LoginScreen,那么你可以通过 setAnimationListener 来实现方法。

    以下是有关它的详细信息:

    public void setAnimationListener(Animation.AnimationListener listener)
    

    它将一个动画监听器绑定到这个动画。动画侦听器会收到动画事件的通知,例如动画的结束或动画的重复。

    参数:

    listener - 要通知的动画监听器

    【讨论】:

    • 无法从“AECS.SplashScreen”转换为“Android.Views.Animations.Animation.IAnimationListener”
    • 这里代码:loadedImage.SetAnimationListener(this);
    【解决方案3】:
    SetContentView(Resource.Layout.SplashScreen);
       ImageView ImageForRotation = FindViewById<ImageView>(Resource.Id.imageForRotation);
    
        var loadedImage = AnimationUtils.LoadAnimation(this,Resource.Animation.SplashScreenImageRotation);
    
    
        ImageForRotation.StartAnimation(loadedImage);
        System.Threading.Thread.Sleep(5000);
        StartActivity(typeof(LoginScreen)); //second activity
    

    【讨论】:

      【解决方案4】:

      创建一个处理程序以在特定时间间隔(例如 5000 毫秒)后切换到下一个活动

      new Handler().postDelayed(new Runnable() {
      
              /*
               * Showing your animation screen with a timer. This will be useful when you
               * want to show case your app logo / company
               */
      
              @Override
              public void run() {
                  // This method will be executed once the timer is over
                  // Start your app main activity
                  Intent i = new Intent(CurrentScreen.this, NextActivity.class);
                  startActivity(i);
      
                  // close this activity
                  finish();
              }
          }, 5000);
      

      在添加处理程序之前编写动画代码。

      【讨论】:

        猜你喜欢
        • 2013-07-05
        • 2013-08-18
        • 1970-01-01
        • 2015-09-15
        • 2017-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多