[英]Android: Pop-up alert while calculations are performed
I'm moving a pc java code to android by using Android Studio. 我正在使用Android Studio将PC Java代码移至android。 The java code performs some numeric calculations routines and in a basic computer it can take few seconds to return the problem solution.
Java代码执行一些数值计算例程,并且在基本计算机上,可能需要几秒钟才能返回问题解决方案。 The java code works very pretty.
Java代码非常漂亮。
By clicking in a button, the routine starts. 通过单击按钮,例程开始。 I'd like to know how to show an elegant "alert" mensagem while the calculations are performed.
我想知道如何在执行计算时显示出优雅的“警报”提示。 A kind of "please wait" in a pop-up.
弹出窗口中的一种“请稍候”。
My basic code: 我的基本代码:
static double h,w,l,t,Te;
static int ambiente;
private EditText numeroin;
//private TextView numeroout;
public void cliqueBotao(View view){
Intent intent = getIntent();
ambiente = intent.getIntExtra("ambiente_selec", 0);
numeroin=(EditText)findViewById(R.id.in_h);
h= Double.parseDouble(numeroin.getText().toString());
numeroin=(EditText)findViewById(R.id.in_w);
w= Double.parseDouble(numeroin.getText().toString());
numeroin=(EditText)findViewById(R.id.in_l);
l= Double.parseDouble(numeroin.getText().toString());
numeroin=(EditText)findViewById(R.id.in_t);
t= Double.parseDouble(numeroin.getText().toString());
numeroin=(EditText)findViewById(R.id.in_T);
Te= Double.parseDouble(numeroin.getText().toString());
(HERE STARTS THE NUMERICAL CALCULATIONS CODE)
/**AlertDialog msg;
msg=new AlertDialog.Builder(this).create();
msg.setMessage("Calculando");
msg.show();*/
}
By the way, in the button xml code the OnClick option is seted to "cliqueBotao". 顺便说一下,在按钮xml代码中,将OnClick选项设置为“ cliqueBotao”。
You should do that math in a worker thread, take a look at AsyncTask . 您应该在辅助线程中进行该数学运算,看看AsyncTask 。 You can use onProgressUpdate and onPostExecute callbacks to control your pop-up dialog lifecycle and even display current calculation progress.
您可以使用onProgressUpdate和onPostExecute回调来控制弹出对话框的生命周期,甚至显示当前的计算进度。
As suggested by Rogério, AsyncTask was the best solution to my problem. 正如Rogério所建议的那样,AsyncTask是解决我的问题的最佳解决方案。 On the other hand, I had to discover how to start a new activity by using AsyncTask.
另一方面,我不得不发现如何使用AsyncTask启动新活动。 It was not easy for me, but a useful solution is here: Android AsyncTask: start new Activity in onPostExecute()
这对我来说并不容易,但是这里有一个有用的解决方案: Android AsyncTask:在onPostExecute()中启动新的Activity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.