java-如何延迟函数的return语句?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何延迟函数的return语句?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1894字,纯文字阅读大概需要3分钟。
内容图文
我有一段代码,其中一个函数打开了一个弹出窗口,以后根据该窗口内发生的事情返回一个整数.但是,当我运行它时,它直接返回整数,而无需打开任何弹出窗口.
我如何告诉该函数等待返回,直到用户执行了某个操作?
这是您需要时的代码:
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;
}
解决方法:
我正在查看您的代码,但在任何地方都看不到调用“ popup.show()”.另外,如果要等待一段时间执行操作,则可以使用以下代码.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// your operations (open popup?)
}
}, **your_delay_time**);
希望对您有帮助!!
内容总结
以上是互联网集市为您收集整理的java-如何延迟函数的return语句?全部内容,希望文章能够帮你解决java-如何延迟函数的return语句?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。