我有下一个代码,我试图根据用户选择在此方法中返回“players”的值。问题我可以用对话框的onClick方法做到这一点,任何想法?
public int dialogoInicial(){
int players = 1;
final String[] playersArray = {"1","2","3","4","5","6"};
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Nº "+getString(R.string.players)+":");
dialog.setSingleChoiceItems(playersArray, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int player) {
players = Integer.parseInt(playersArray[player]);
}
});
dialog.show();
return players;
}
答案 0 :(得分:1)
这里的问题是在显示对话框时将执行return players
。简单的解决方案是使players
变量成为类级变量而不是函数的局部变量。您还可以使函数返回void
,因为它无法返回任何内容。 Android对话框为Asynchronous
。