首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用AlertDialog和EditText,等待用户响应

使用AlertDialog和EditText,等待用户响应
EN

Stack Overflow用户
提问于 2014-05-21 06:51:58
回答 1查看 608关注 0票数 0

我正在使用AlertDialog设计函数,使其在VB6中充当一个InputBox。但是,使用下面的代码,AlertDialog不会返回值。

似乎该函数在用户输入之前返回。

代码语言:javascript
运行
复制
public String InputBox(Context srcpage, String Msg, String Title, String InputDataType_TXT_or_NUM)
    {

        ResetReturnText();
        AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(srcpage);

        dlgAlert.setMessage(Msg);
        dlgAlert.setTitle(Title);

        // Set an EditText view to get user input 
        final EditText inputTxt = new EditText(srcpage);


        //Set the input type of the EditText
        //-------------------------------------------------------
        if(InputDataType_TXT_or_NUM=="TXT")
        inputTxt.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);

        else if(InputDataType_TXT_or_NUM=="NUM")
        inputTxt.setInputType(InputType.TYPE_CLASS_NUMBER);

        else
        return "Invalid Data Type";
        //-------------------------------------------------------

        dlgAlert.setView(inputTxt);

        dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          ReturnText = inputTxt.getText().toString();
          }
        });

        dlgAlert.setNegativeButton("Cancel",null);
        dlgAlert.create().show();

        return ReturnText;
    }

我希望这些代码使用函数调用返回值,如:

代码语言:javascript
运行
复制
  String NewProvince="";
  NewProvince = InputBox(this, "Province:", "Add/Edit Province", "TXT");
  Log.w("Input:",NewProvince);

我能请你们帮我解决这个问题吗?

谢谢!

EN

Stack Overflow用户

回答已采纳

发布于 2014-05-21 07:31:52

您必须使用不同的方法,基于“回调”。

因此,您的函数只需返回void并在onClick侦听器中执行操作。

代码语言:javascript
运行
复制
private void buildDialog() {
    // build your dialog with everything
    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // do what you want with the value
            doSomethingWithValue(value);
        }
    });
}

protected void doSomethingWithValue(String value) {
    Log.i("MyTAG", value);
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23775950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档