2016-06-12 105 views
0

我正在使用Android Studio將一個pc java代碼移動到android。 java代碼執行一些數字計算例程,並且在基本計算機中可能需要幾秒鐘才能返回問題解決方案。 Java代碼非常漂亮。Android:執行計算時彈出警報

通過點擊按鈕,程序開始。我想知道如何在執行計算時顯示一個優雅的「警報」mensagem。彈出窗口中的一種「請稍等」。

我的基本代碼:

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();*/ 

} 

順便說一句,在按鈕XML代碼的OnClick選項seted爲 「cliqueBotao」。

回答

0

你應該在工作線程中做這個數學,看看AsyncTask。您可以使用onProgressUpdate和onPostExecute回調來控制彈出對話框的生命週期,甚至可以顯示當前的計算進度。

+0

我不知道AsyncTask類。我是一個「黑屏和白色人物」程序員。我會研究你的建議。 TKS! –

+0

複雜微積分等任何大而緩慢的工作應該在主UI線程之外完成,這就是AsyncTask的用武之地。如果您能夠通過此API建議達到您的需求,請接受我的答案!問候。 –

+0

不錯!但是,發現如何在AsyncTask類中開始新的活動真的是一場戰鬥。最好的答案在這裏:http://stackoverflow.com/questions/12332930/android-asynctask-start-new-activity-in-onpostexecute –

0

正如Rogério所建議的,AsyncTask是解決我的問題的最佳解決方案。另一方面,我不得不通過使用AsyncTask來發現如何開始一個新的活動。這對我來說並不容易,但一個有用的解決方案在這裏:Android AsyncTask: start new Activity in onPostExecute()