Java 使用AlertDialog同步获取文本

Java 使用AlertDialog同步获取文本,java,android,text,dialog,synchronous,Java,Android,Text,Dialog,Synchronous,如何使用AlertDialog同步获取文本?例如,我想做这样的事情: public String GetTextDialog() { final EditText text = new EditText(activity); // Gets the chat final AlertDialog.Builder dialog = new AlertDialog.Builder(activity); dialog.setView(text

如何使用AlertDialog同步获取文本?例如,我想做这样的事情:

  public String GetTextDialog()
  {          
    final EditText text = new EditText(activity);

    // Gets the chat
    final AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
    dialog.setView(text);
    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
        }
    });
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) { 
        } 
    });

    // Display
    dialog.show();

    // Return text after dialog is complete
    return text.getText().toString();
  }
private void GetUsernameDialog()
{
    ShowTextDialog(
            "Enter Username",                                                   // Title
            username,                                                           // Default value
            new TextHandler()                                                   // Positive button
            {
                public String Title() { return "Ok"; }
                public void HandleText(String text) { SetUsername(text); }
            }, 
            new TextHandler()                                                   // Negative button
            {
                public String Title() { return "Cancel"; }
                public void HandleText(String text) { }
            }, 
            null,                                                               // Neutral button
            200                                                                 // Width
        );
}

<强>编辑< /强>:你会认为这是一个更好的方式:

interface TextHandler 
{ 
    public String Title(); 
    public void HandleText(String text); 
}

public static boolean ShowTextDialog(
        String title,
        String defaultValue,
        final TextHandler posButton,
        final TextHandler negButton,
        final TextHandler neutButton,
        int width
        )
{
    // Check for already existing dialog
    if(showDialog) return false;
    showDialog = true;

    // Verify context
    if(context == null) 
    {
        Log.e("Crystal", "Screen::ShowTextDialog No Context!");
        showDialog = false;
        return false;
    }

    // Verify we have buttons
    if(neutButton == null)
    {
        if(posButton == null || negButton == null)
        {
            Log.e("Crystal", "Screen::ShowTextDialog Must supply both postive and negative buttons!");
            showDialog = false;
            return false;
        }
    }
    else
    {
        if(posButton != null || negButton != null)
        {
            Log.e("Crystal", "Screen::ShowTextDialog Can't have neutral button with other type!");
            showDialog = false;
            return false;
        }
    }

    // Create the dialog
    AlertDialog.Builder dialog = new AlertDialog.Builder(context);
    if(title != null) dialog.setTitle(title);

    // Create the chat
    final EditText text = new EditText(context);
    text.setSingleLine();
    if(defaultValue != null) text.setText(defaultValue);
    if(width != 0) text.setWidth((int)(width*context.getResources().getDisplayMetrics().density));

    // Add text to dialog
    dialog.setView(text);

    if(posButton != null)
    {
        dialog.setPositiveButton(posButton.Title(), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                posButton.HandleText(text.getText().toString());
                showDialog = false;
            }
        });
    }

    if(negButton != null)
    {
        dialog.setNegativeButton(negButton.Title(), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                negButton.HandleText(text.getText().toString());
                showDialog = false;
            }
        });
    }

    if(neutButton != null)
    {
        dialog.setNeutralButton(neutButton.Title(), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                neutButton.HandleText(text.getText().toString());
                showDialog = false;
            }
        });
    }

    // Display
    dialog.show();

    return true;
}
并这样称呼:

  public String GetTextDialog()
  {          
    final EditText text = new EditText(activity);

    // Gets the chat
    final AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
    dialog.setView(text);
    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
        }
    });
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) { 
        } 
    });

    // Display
    dialog.show();

    // Return text after dialog is complete
    return text.getText().toString();
  }
private void GetUsernameDialog()
{
    ShowTextDialog(
            "Enter Username",                                                   // Title
            username,                                                           // Default value
            new TextHandler()                                                   // Positive button
            {
                public String Title() { return "Ok"; }
                public void HandleText(String text) { SetUsername(text); }
            }, 
            new TextHandler()                                                   // Negative button
            {
                public String Title() { return "Cancel"; }
                public void HandleText(String text) { }
            }, 
            null,                                                               // Neutral button
            200                                                                 // Width
        );
}

您可以在另一个线程中运行该对话框,其中包含一个循环器,在对话框终止时终止循环器,让主UI线程等待另一个线程。。。但这是一个如此复杂的设计,如果你这么做,上帝可能会杀了一只小猫。请问您为什么需要同步数据访问?这只是为了方便,还是你有设计的理由


如果是关于在对话框设置之前访问状态,那么嵌套的匿名类就是答案。你已经在上面的片段中有了这些;它们可以自动访问嵌套函数的
最终
变量和嵌套类的成员。

我想要一个可以返回从用户检索到的文本的函数。如果有更好的东西,我不需要使用AlertDialog。GUI本质上是异步的(事件驱动的)。此外,与Windows不同,Android无法在消息循环中运行消息循环。我知道你想要什么;问题是-为什么?我想我可能可以重新设计我的代码,使之成为异步的,但我希望有一个简单的函数可以调用,以便能够获取文本。如果没有简单的方法,我可以尝试另一种方法。事件驱动编程有什么困难?习惯它吧,在Android(或任何GUI系统)中,它无处不在。仍然是异步的。该字符串在GetUsernameDialog()返回后很长时间才可用。你已经抽象出了一些对话内容;但这并不是你最初提出的问题的答案。