Android 如何使AlertDialogs连续显示和取消显示?

Android 如何使AlertDialogs连续显示和取消显示?,android,xamarin.android,android-alertdialog,android-thread,Android,Xamarin.android,Android Alertdialog,Android Thread,我希望带有消息“1”的AlertDialog出现在屏幕上,3秒钟后消失,然后我希望另一个带有消息“2”的AlertDialog出现在屏幕上,3秒钟后消失,依此类推直到5。我有以下代码: [Activity(Label = "TestAlertDialogBuilder", MainLauncher = true)] public class MainActivity : Activity { protected override void OnCreate(Bundle savedIns

我希望带有消息“1”的AlertDialog出现在屏幕上,3秒钟后消失,然后我希望另一个带有消息“2”的AlertDialog出现在屏幕上,3秒钟后消失,依此类推直到5。我有以下代码:

[Activity(Label = "TestAlertDialogBuilder", MainLauncher = true)]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

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

        for (int i=0; i <=5 ; i++)
        {
            //making the program stop so I can see the alert showing and dissappearing
            System.Threading.Thread.Sleep(3000);
            ShowAlert(i);
        }
    }

    Dialog dialog;
    public void ShowAlert(int i)
    {
        if (dialog != null)
        {
            dialog.Cancel();
        }
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        alertDialog.SetMessage(i.ToString());
        dialog = alertDialog.Create();
        dialog.Show();
    }
}
[活动(Label=“TestAlertDialogBuilder”,MainLauncher=true)]
公共课活动:活动
{
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);

for(int i=0;i问题是由于在执行for循环之前阻塞了UI主线程。所以为对话框创建单独的线程并在UI中显示它

试试这个

Thread thread = new Thread(new Runnable() {

        int i = 0;
        @Override
        public void run() {
            for (i=0; i <=5 ; i++)
            {
                //making the program stop so I can see the alert showing and dissappearing
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ShowAlert(i);
                    }
                });

            }

        }
    });
   thread.start();

 public void ShowAlert(int i)
{
    if (dialog != null)
    {
        dialog.cancel();
    }
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    alertDialog.setMessage("" + i);
    dialog = alertDialog.create();
    dialog.show();
}
Thread-Thread=新线程(new Runnable()){
int i=0;
@凌驾
公开募捐{

对于(i=0;i只需将Mohamed Mohaideen AH的答案转换为C#语言即可

public class MyRunnable : Java.Lang.Object, Java.Lang.IRunnable
{
    private MainActivity mainActivity;

    public MyRunnable(MainActivity mainActivity)
    {
        this.mainActivity = mainActivity;
    }

    public void Run()
    {
        for (int i = 0; i <= 5; i++)
        {
            try
            {
                Java.Lang.Thread.Sleep(3000);
            }
            catch (Java.Lang.Exception e)
            {
                e.PrintStackTrace();
            }
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.ShowAlert(i);
            });
        }
    }
}


[Activity(Label = "App5", MainLauncher = true)]
public class MainActivity : Activity
{

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

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


        MyRunnable myRunnable = new MyRunnable(this);
        Java.Lang.Thread thread = new Java.Lang.Thread(myRunnable);
        thread.Start();

    }
    Dialog dialog;
    public void ShowAlert(int i)
    {
        if (dialog != null)
        {
            dialog.Cancel();
        }
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        alertDialog.SetMessage("AlertDialog: " + i);
        dialog = alertDialog.Create();
        dialog.Show();
    }
}
公共类MyRunnable:Java.Lang.Object、Java.Lang.IRunnable { 私人活动; 公共MyRunnable(MainActivity MainActivity) { this.mainActivity=mainActivity; } 公开募捐 { 对于(int i=0;i { 主要活动。ShowAlert(i); }); } } } [活动(Label=“App5”,MainLauncher=true)] 公共课活动:活动 { 创建时受保护的覆盖无效(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); //从“主”布局资源设置视图 SetContentView(Resource.Layout.Main); MyRunnable MyRunnable=新的MyRunnable(此); Java.Lang.Thread线程=新的Java.Lang.Thread(myRunnable); thread.Start(); } 对话; 公共虚空显示警报(int i) { 如果(对话框!=null) { dialog.Cancel(); } AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(此); SetMessage(“alertDialog:+i”); dialog=alertDialog.Create(); dialog.Show(); } }
为什么需要使用C#你是否尝试过混合开发。因为我在Xamarin.Android中编程,我知道Xamarin.Android,但我现在没有操场要检查,所以标记Xamarin Android以引起Xamarin用户的注意。我应该将此代码放在我想使用它的活动的内部还是外部?它可以工作。唯一的问题是它跳过了
警报对话框:1
一直持续到
警报对话框:6