0

我有一个方法,当用户点击我的创建按钮时调用它,它调用一个方法名称 createEntry();

在此方法中,它会经过多次检查并根据结果返回 toasts 或错误,当它运行正确的结果时,它总是带我回到我的家庭活动,但如果出现错误,我希望它留在当前屏幕上,如何我可以这样做吗?

任何人都可以给我一些帮助吗?

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.create_button:
        Intent i = new Intent(this, Home.class);
                    createEntry();// this is where my error checking is done so what I would like to know is how can i stay on the current screen for certain errors
        startActivity(i);
        break;
}
}

    }

如果出现错误,我不想加载主类,我希望它保留在当前屏幕上。

4

3 回答 3

0

如果发生错误,您createEntry()应该返回一个布尔值。有了它,您可以指示是否应该开始新活动。

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.create_button:
            if (createEntry()) { // return true for no errors
                Intent i = new Intent(this, Home.class);
                startActivity(i);
            }
            break;
    }
}
于 2012-05-01T19:08:42.803 回答
0

我猜你应该返回一个关于你是否有错误要修复的布尔值:

if(createEntry() == true) {
    Intent i = new Intent(this, Home.class);
    startActivity(i);
}
于 2012-05-01T19:08:19.563 回答
0

检查错误,如果发现任何错误,然后从该方法返回:

if (!createEntry()) return;
于 2012-05-01T19:37:35.087 回答