gpt4 book ai didi

java - 局部变量在 OnClickListener 中不可见

转载 作者:太空狗 更新时间:2023-10-29 15:54:13 24 4
gpt4 key购买 nike

我在开发 Android 应用程序时遇到问题:

我有这个方法:

// User has introduced an incorrect password.
private void invalidPassword()
{
// R.id.string value for alert dialog title.
int dialogTitle = 0;
// R.id.string value for alert dialog message.
int dialogMessage = 0;
boolean hasReachedMaxAttempts;

clearWidgets();
numIntents++;
hasReachedMaxAttempts = (numIntents > maxNumIntents);

// Max attempts reached
if (hasReachedMaxAttempts)
{
dialogTitle = R.string.dialog_title_error;
dialogMessage = R.string.dialog_message_max_attempts_reached;
}
else
{
dialogTitle = R.string.dialog_title_error;
dialogMessage = R.string.dialog_message_incorrect_password;
}

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(dialogMessage)
.setTitle(dialogTitle);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
// TODO: User clicked OK button
if (hasReachedMaxAttempts)
{
}
else
{
}
}
});

AlertDialog dialog = builder.create();
dialog.show();
}

如何在 onClick 中显示 boolean hasReachedMaxAttempts;

最佳答案

您需要该变量是最终的;

final boolean hasReachedMaxAttemptsFinal = hasReachedMaxAttempts;
AlertDialog.Builder builder = new AlertDialog.Builder(this);

if (hasReachedMaxAttemptsFinal)

关于java - 局部变量在 OnClickListener 中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16935259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com