gpt4 book ai didi

android - Xamarin - Android - 启动画面(多个)

转载 作者:行者123 更新时间:2023-11-30 02:24:55 25 4
gpt4 key购买 nike

我刚开始使用 xamarin 创建我的第一个 Android 应用程序,但遇到了一个小问题。

我想在应用程序的开头有 2 个启动画面。

我创建了第一个工作正常的,然后是第二个,然后是 mainActivity。

但是由于某些原因,它没有显示第二个初始屏幕。

如果我在第二行中删除这一行,那么它可以工作但不会转到 mainActivity。

StartActivity(typeof(MainActivity));

主 Activity .cs

[Activity(Theme = "@style/Theme.Splash1", MainLauncher = true, NoHistory = true)]
public class SplashActivity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

Thread.Sleep(3000);

StartActivity(typeof(SplashActivity2));
}
};

[Activity(Theme = "@style/Theme.Splash2", NoHistory = true)]
public class SplashActivity2 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

Thread.Sleep(3000);

StartActivity(typeof(MainActivity));
}
};

[Activity (Label = "coco1_droid", Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;

protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);

// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);

button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
};
}
}

最佳答案

问题在于,因为您正在使用 Thread.Sleep(3000) 休眠 UI 线程,所以 UI 卡住并且新的 Activity 在 OnCreate 方法返回之前启动.

我建议使用计时器(例如:System.Timers.Timer)在开始新 Activity 之前等待三秒钟。这样 UI 就不会卡住并且 OnCreate 方法返回。

我已经根据我的建议修改了您的示例:

主 Activity .cs

[Activity(Theme = "@style/Theme.Splash1", MainLauncher = true, NoHistory = true)]
public class SplashActivity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

Timer timer = new Timer();
timer.Interval = 3000; // 3 sec.
timer.AutoReset = false; // Do not reset the timer after it's elapsed
timer.Elapsed += (object sender, ElapsedEventArgs e) =>
{
StartActivity(typeof(SplashActivity2));
};
timer.Start();
}
};

[Activity(Theme = "@style/Theme.Splash2", NoHistory = true)]
public class SplashActivity2 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

Timer timer = new Timer();
timer.Interval = 3000; // 3 sec.
timer.AutoReset = false; // Do not reset the timer after it's elapsed
timer.Elapsed += (object sender, ElapsedEventArgs e) =>
{
StartActivity(typeof(MainActivity));
};
timer.Start();
}
};

[Activity (Label = "coco1_droid", Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;

protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);

// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);

button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
};
}
}

关于android - Xamarin - Android - 启动画面(多个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28002562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com