在android中打开警报对话框时如何等待用户输入

时间:2014-11-07 22:26:22

标签: java android multithreading alertdialog ui-thread

好的,所以我写了这段代码,我用一个线程打开一些警告对话框。问题是线程在打开第二个对话框之前不会等待第一个对话框关闭。正如您在代码中看到的那样,在打开两个对话框之间使用了wait(),并在对话框中按钮的onClick事件中使用notify()。我使用runOnUidThread()函数来显示创建的对话框。出于某种原因,当我打开活动时,对话框将无法打开,应用程序将出现黑屏,应用程序将最终停止工作。当我注释掉wait()语句(包括try和catch语句)时,活动会显示彼此之后的警报对话框。那么甚至可以等待这样的用户输入吗?或者我做了一些完全错误的事情。谢谢。 HEre是我的代码:

public class EditTagActivity extends Activity
{   
    AlertDialog alertDialog1, alertDialog2;

    Thread dialogManager = new Thread()
    {
        @Override
        public void run()
        {
            runOnUiThread(showDialog1);
            try 
            {
                synchronized(dialogManager)
                {
                    Log.d("Threads", "wait()");
                    dialogManager.wait();
                }
            } 
            catch (InterruptedException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            runOnUiThread(showDialog2);
            try 
            {
                synchronized(dialogManager)
                {
                    Log.d("Threads", "wait()");
                    dialogManager.wait();
                }
            } 
            catch (InterruptedException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
     };

    Thread showDialog1 = new Thread()
    {
        @Override
        public void run() 
        {
            Log.d("Threads", "alertDialog1.show();");
            alertDialog1.show();            
        }
    };

    Thread showDialog2 = new Thread()
    {
        @Override
        public void run() 
        {
            Log.d("Threads", "alertDialog2.show();");
            alertDialog2.show();            
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_tag);
        Log.d("Threads", "setup()");
        setup();
    }

    void setup() 
    {
        Log.d("Threads", "g.run()");
        createAlertDialog();
        dialogManager.run();
    }

    void createAlertDialog() 
    {
        Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Alert");
        alert.setMessage("Flaq");
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                synchronized(dialogManager)
                {
                    dialogManager.notifyAll();
                }
            }
        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                synchronized(dialogManager)
                {
                    dialogManager.notifyAll();
                }
            }
        });
        alertDialog1 = alert.create();
        alertDialog2 = alert.create();
    }
}

2 个答案:

答案 0 :(得分:2)

是的,只需在第一个对话框的setPositiveButton的onClick中调用第二个对话框的dialog.show()。这样,第二个对话框将被迫等到第一个对话框被解除。

答案 1 :(得分:0)

为什么使用线程等待用户输入?必须在UI线程上运行一个对话框,如android培训指南中所述:“每个应用程序都有自己的特殊线程,运行UI对象,如View对象;此线程称为UI线程。

对话框将占用UI线程,直到它被解除为止。在关闭时,您可以在后台线程上处理用户输入。

以下是一些很好的参考资料:

https://developer.android.com/training/multiple-threads/communicate-ui.html

http://developer.android.com/guide/topics/ui/dialogs.html

void createAlertDialog() 
    {
        Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Alert");
        alert.setMessage("Flaq");
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                       /* 
                       Handle user input on other thread. With for example AsyncTask 
                       */
                       alertDialog2 = alert.create();
            }
        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                /* 
                Handle user input on other thread. With for example AsyncTask 
                */
            }
        });
        alertDialog1 = alert.create();
    }
相关问题