安卓:等待用户输入的对话框?对话框、用户

2023-09-12 07:19:39 作者:for granted(理所当然)

我想实现显示一个对话框,等待,直到被关闭对话框,然后返回根据对话内容而导致的一种方法。这可能吗?

 公共字符串getUserInput()
{
    //做一些事情,以显示对话框
    字符串输入= //从对话框中输入
    返回输入;
}
 

我其实是想实现有方法的接口公共字符串getUserInput(),其中返回的字符串必须通过对话获得。这很容易在java中完成的,在Android的似乎是不可能的?

编辑:发布一些示例code的要求,在注释

getInput()必须调用从后台线程(我从AsynchTask调用它)。 getInput()显示一个对话框,并调用wait。当确定按钮pssed对话框上的$ P $,对话框设置用户输入一个成员变量和电话通知。当通知被调用,getInput()继续并返回成员变量。

 字符串m_Input;

公共同步串getInput()
{
    runOnUiThread(新的Runnable()
    {
        @覆盖
        公共无效的run()
        {
            AlertDialog.Builder警报=新AlertDialog.Builder(上下文);
            //自定义警告对话框,允许所需的输入
            alert.setPositiveButton(OK,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释whichButton)
            {
                          m_Input = alert.getCustomInput();
                          通知();
            }
        });
        alert.show();
        }
    });

    尝试
    {
         等待();
    }
    赶上(InterruptedException的E)
    {
    }

    返回m_Input;
}
 

解决方案

感谢所有的反馈意见,我可以使用一个后台线程同时等待来解决这个()和notify()。我承认这不是给定的模式最大的想法,但必须符合,我有工作的图书馆。

在windows系统中不属于对话框的组成元素是

I would like to implement a method that displays a dialog, waits until the dialog is dismissed, and then returns a result depending on the dialog contents. Is this possible?

public String getUserInput()
{
    //do something to show dialog
    String input = //get input from dialog
    return input;
}

I am actually trying to implement an interface which has method "public String getUserInput()", where the returned String must be retrieved via dialog. This is easily done in java, seems impossible in android?

EDIT: Posting some sample code as requested in comment

getInput() must be called from a background thread (I call it from an AsynchTask). getInput() displays a dialog and calls wait. When the ok button is pressed on the dialog, the dialog sets the user input in a member variable and calls notify. When notify is called, getInput() continues and returns the member variable.

String m_Input;

public synchronized String getInput()
{
    runOnUiThread(new Runnable() 
    {
        @Override
        public void run() 
        {
            AlertDialog.Builder alert = new AlertDialog.Builder(context);
            //customize alert dialog to allow desired input
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton)
            {
                          m_Input = alert.getCustomInput();
                          notify();
            }
        });
        alert.show();   
        }
    });

    try 
    {
         wait();
    } 
    catch (InterruptedException e) 
    {
    }

    return m_Input;
}

解决方案

Thanks for all the feedback, I was able to solve this using a background thread along with a wait() and notify(). I recognize this isn't the greatest idea for the given paradigm, but it was necessary to conform to a library that I am working with.