【问题标题】:Return int from dialog Android从对话框 Android 返回 int
【发布时间】:2012-06-04 12:56:13
【问题描述】:

我有下一个代码,我试图根据用户选择在此方法中返回“玩家”的值。我可以用对话框的 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;
}

【问题讨论】:

    标签: android dialog return


    【解决方案1】:

    这里的问题是return players 将在显示对话框时执行。简单的解决方案是使players 变量成为类级别变量,而不是函数的局部变量。你也可以让你的函数返回void,因为它不能返回任何东西。 Android 对话框是Asynchronous

    【讨论】:

    • 是的,它是真的..我必须放松一下!哈哈哈谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多