你如何延迟函数的return语句?

时间:2015-07-13 11:14:20

标签: java android

我有一段代码,其中一个函数打开一个弹出窗口,以便稍后根据该窗口内发生的内容返回一个整数。但是当我运行它时,它直接返回整数而不打开任何弹出窗口。

如何判断该功能在用户完成某项操作之前等待返回?

以下是您需要的代码:

public int initPopup(String monsterName, String monsterHP){
    final int monsterHPInt = Integer.parseInt(monsterHP);
    PopupWindow popup;
    TextView popupText;
    Button closePopupButton;
    final SeekBar monsterHPChanger;
    LinearLayout popupLayout;

    popupText = new TextView(this);
    popupText.setText(monsterName);

    monsterHPChanger = new SeekBar(this);
    monsterHPChanger.setMax(monsterHPInt);
    /** Will only use if necessary
     *  monsterHPChanger.setProgress(monsterHPChanger.getMax());
     */

    popupLayout = new LinearLayout(this);
    popupLayout.setOrientation(LinearLayout.VERTICAL);
    popupLayout.addView(popupText);
    popupLayout.addView(monsterHPChanger);
    //TODO: Create the layout of the popup and the popup itself
    popup = new PopupWindow(popupLayout, LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    popup.setContentView(popupLayout);

    //Creating encapsulation class to edit the monsterHP with the value of the SeekBar
    final MonsterHP monsterHPObject = new MonsterHP(monsterHPInt, monsterHPChanger.getProgress());
    closePopupButton = new Button(this);
    closePopupButton.setId(R.id.closePopup);
    closePopupButton.setText("Ok");

    closePopupButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            monsterHPObject.update(monsterHPChanger.getProgress());
        }
    });

    Log.println(1, "Method", "Returns " + monsterHPObject.getHP());
    // TODO: reactivate when debug is done return monsterHPObject.getHP();
    //Returning 0 for debug reasons
    return 0;

}

2 个答案:

答案 0 :(得分:2)

我正在寻找你的代码而我看不到电话" popup.show()"任何地方。此外,如果您想等待一段时间来执行操作,可以使用以下代码。

# Evaluate planes
n <- 20
x <- y <- seq(-1, 1, length = n)
region <- expand.grid(x = x, y = y)

z1 <- matrix(-(region$x + region$y), n, n)
z2 <- matrix(-region$x + region$y, n, n)
z3 <- matrix(region$x - region$y - 0.8, n, n)

surface3d(x, y, z1, back = 'line', front = 'line', col = 'red', lwd = 1.5, alpha = 0.4)
surface3d(x, y, z2, back = 'line', front = 'line', col = 'orange', lwd = 1.5, alpha = 0.4)
surface3d(x, y, z3, back = 'line', front = 'line', col = 'blue', lwd = 1.5, alpha = 0.4)
axes3d()

希望它可以帮到你!!

答案 1 :(得分:2)

使用传递给方法的回调接口。 当用户执行操作时,请调用适当的回调方法。