我正在使用AlertDialog设计函数,就像VB6中的InputBox一样。但是,使用下面的代码,AlertDialog不会返回值。
似乎函数在用户输入之前返回。
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;
}
我希望这些代码通过函数调用返回值,如:
String NewProvince="";
NewProvince = InputBox(this, "Province:", "Add/Edit Province", "TXT");
Log.w("Input:",NewProvince);
我可以请你们帮我解决这个问题吗?
谢谢!
答案 0 :(得分:0)
您必须使用基于“回调”的其他方法。
因此,您的函数只需返回void
并在onClick侦听器中执行操作。
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);
}