2011-11-02 83 views
1

我正在嘗試爲我的手機編寫一個非常基本的應用程序,只是爲了讓自己回到編程的搖擺之中。我已經掌握了我想要做的框架和基礎知識,我只需要一小段幫助我的工作。Android - 動作進度對話框退出

在我的應用程序中,我有一個按鈕。當按下該按鈕時,我希望應用程序將一段文本更改爲「決定...」,彈出一個不確定的進度條,然後打印另一個活動的結果,但僅在對話框已經退出。目前我正在使用線程,但無法正常工作。

有沒有人有任何想法如何讓它按我想要的方式工作?

這是我的活動文件來源:

package com.bassios.decisionengine; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

public class DecisionEngineActivity extends Activity { 

private ProgressDialog progressDialog; 
public String decision; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

} 

public void decide(View view) { 
    TextView tv = (TextView) findViewById(R.id.Decision1); 
    tv.setText("Deciding..."); 
    int a = generate(); 
    decision = decision(a); 
    runDialog(1); 
//  alterText(); 
} 

public void alterText() { 
    TextView tv = (TextView) findViewById(R.id.Decision1); 
    tv.setText(decision); 
} 

private void runDialog(final int seconds) { 
    progressDialog = ProgressDialog.show(this, "Please wait...", "Deciding..."); 
    new Thread(new Runnable() { 

     public void run() { 
      try { 
       Thread.sleep(seconds * 1000); 
       progressDialog.dismiss(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 
} 

public static int generate() { 
    int a = (int) (Math.random() * 10) + 1; 
    return a; 
} 

public static String decision(int a) { 
    String decision = null; 
    switch (a) { 
     case 1: 
      decision = "Yes"; 
      break; 
     case 2: 
      decision = "No"; 
      break; 
     case 3: 
      decision = "Yes"; 
      break; 
     case 4: 
      decision = "No"; 
      break; 
     case 5: 
      decision = "Yes"; 
      break; 
     case 6: 
      decision = "No"; 
      break; 
     case 7: 
      decision = "Yes"; 
      break; 
     case 8: 
      decision = "No"; 
      break; 
     case 9: 
      decision = "Yes"; 
      break; 
     case 10: 
      decision = "No"; 
      break; 
    } 
    return decision; 
    } 
} 

謝謝!

+0

嘗試使用'AsyncTask'而不是'Thread',你不能改變另一個'Thread'的UI狀態 –

回答

1

使用AsyncTask。

一個例子:

http://javatech.org/2011/02/discovering-android-opening-a-progress-dialog-with-asynctask/

更新:

首先,你必須聲明類是這樣的:

private class DecideTask extends AsyncTask<Void, Void, Boolean> 
{ 
... 
} 

我不知道該怎麼showDialogremoveDialog的作品,但我會這樣做,而不是:

顯示對話框(在onPreExecute):

progressDialog = ProgressDialog.show(this, "Please wait...", 
       "Deciding...", true); 

卸下/駁回對話框(在onPostExecute):

progressDialog.dismiss(); 

聲明這三個方法:

protected Boolean doInBackground(Void... params) 

protected void onPreExecute() 

protected void onPostExecute(Boolean result) 

應做到這一點。

+0

我試圖使用這個例子,但是當我粘貼它來改變它適合的時候,我得到這個: com.bassios.decisionengine.DecisionEngineActivity.decideTask不是抽象的,不會覆蓋android.os.AsyncTask中的抽象方法doInBackground(java.lang.Object ...) – Bassios

+0

你可以發佈你的代碼嗎? – eightx2

+0

我已經把它放在PasteBin上,因爲它太長了,不適合這裏的註釋:http://pastebin.com/2BpZssjV – Bassios