1
The function below returns a password typed in AlertDialog
and is in a class responsible for creating any dialog box in my app, but as a whole AlertDialog
the app does not "wait" the return of the function to continue the execution. My question is: how to make the app wait for the function to return to continue executing the rest of the code?
public String pass() {
final String[] result = new String[1];
final EditText txtUrl = new EditText(activity);
// Set the default text to a link of the Queen
txtUrl.setHint("digite a senha");
new AlertDialog.Builder(activity)
.setTitle("Senha")
.setMessage("Digite a senha para liberar o acesso")
.setView(txtUrl)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
result[0] = txtUrl.getText().toString();
}
})
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.show();
return result[0];
}
There is a this class, which does exactly that, but however not a good practice, do not recommend to use. Java is different for example from C# and other languages, you can pass a
listener
for its function.– Marco Giovanni
Look at this reply. If you don’t want to use a Dialogfragment (I recommend you use it), see Fragment as your class and method
onAttach()
as your builder.– ramaral
@ramaral poe an example of Dialogfragment as a response so I can positive it
– Benjamim Mendes Junior
Did you understand what is in the other answer? If yes and that is what you were looking for I vote for this question as a duplicate of the other. Remember that you can always vote on any answer/question if you think it was helpful.
– ramaral
Yes understood and also Fis some queries from the information passed there. You can mark as repeated. Thank you.
– Benjamim Mendes Junior