java - 使用静态变量Android更新时间

标签 java android dialog static-members

我正在使用菜单选项来设置游戏时间。默认情况下,时间设置为 10 秒。用户可以通过单击菜单然后选择一个选项来设置时间。我使用的是自定义 View ,因此更改时间的方法与 View 位于不同的类中。

当用户单击菜单选项时,会出现一个对话框,其中包含一个 EditText 作为其 View 。用户输入 5 到 60 之间的数字。我必须等待整个游戏周期才能更改时间,因此它应该在下一场游戏中更改。

但事实并非如此..

只有当我尝试再次更改时间时,它才会改变。

例如)

我第一次玩时将时间更改为 5 秒,预计下一个游戏周期会更改为 5 秒。在下一个游戏周期中,它不会更改为 5。它将保持在上一个时间。我再次将时间更改为 30 秒。下一个游戏周期,计时器现在显示为 5 秒。如果我再次将时间更改为 40,则会显示 30。

这是我在每个 newgame() 上更改时间的地方;

static int timeRemaining = 10;
public void newGame() {
    timeLeft = timeRemaining; // start the countdown
    // do other stuff

这是我要求用户输入并更改变量 timeRemaining 的地方。请记住,他们属于不同的类(class)。

@Override
public boolean onOptionsItemSelected(MenuItem item) {


    int id = item.getItemId();

    if (id == R.id.action_settings) {       
            AlertDialog.Builder dialog;
            dialog = new AlertDialog.Builder(this);
            final EditText input = new EditText(this);

            dialog.setTitle("Enter the time limit");
            dialog.setView(input);
            dialog.setPositiveButton("Done", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    getInput = input.getText().toString();
                    try{
                    result = Integer.parseInt(getInput);

                    }catch(InputMismatchException e){
                        CannonView.timeRemaining = 10;
                        Toast.makeText((Context) getApplicationContext(), "Enter an integer", Toast.LENGTH_SHORT).show();
                    }
                    if(result < 5){
                        Toast.makeText((Context) getApplicationContext(), "Invalid input", Toast.LENGTH_SHORT).show();

                    } else if (result > 60) {
                        Toast.makeText((Context) getApplicationContext(), "Invalid input", Toast.LENGTH_SHORT).show();

                    }
                    Toast.makeText((Context) getApplicationContext(), "Time set changed next game", Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog box = dialog.create();
            box.show();
            if(result < 5 || result > 60){      
                CannonView.timeRemaining = 10;
                return true;
            }else{
                CannonView.timeRemaining = result;
                return true;
            }       
    }

    return super.onOptionsItemSelected(item);
}

我在这里更改了 timeRemaining,但直到我再次更改它才更新。有什么建议么?

最佳答案

我认为错误是您没有将以下内容放入 onClickListener

if(result < 5 || result > 60)      
    CannonView.timeRemaining = 10;
else  CannonView.timeRemaining = result;

应该是这样的

dialog.setPositiveButton("Done", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // your original code
        if(result < 5 || result > 60)      
            CannonView.timeRemaining = 10;
        else  CannonView.timeRemaining = result;
});

关于java - 使用静态变量Android更新时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29024227/

相关文章:

android - 将jar文件与phonegap/cordova插件一起使用

dialog - 如何抑制不需要的 InstallShield InstallScript MSI 对话框?

android - 如果屏幕关闭,如何更新时钟小部件?

java - 在 Java 中创建 NON DISTINCT 值子集的所有多重集

java - 从常量文件访问常量值到 JSP 文件

java - 如何在java中用 "\"替换 "\\"

android - 扩展 Context 是不是一个坏主意?安卓

android - 不显示警报对话框

angular - 我如何制作 MatDialog 可拖动/Angular Material

java - Java 中的泛型