Android - 进度操作对话框退出

时间:2011-11-02 12:36:31

标签: java android progressdialog

我正在尝试为我的手机编写一个非常基本的应用程序,只是为了让自己回到编程的摇摆中。我已经掌握了我想要做的框架和基础知识,我只是需要帮助一小部分让我失望。

在我的应用上,我有一个按钮。当按下该按钮时,我希望应用程序将一段文本更改为“决定...”,弹出一个不确定的进度条一两秒然后退出,然后打印另一个活动的结果,但仅在对话框之后退出了。我目前正在使用一个线程,但我无法使其正常运行。

有没有人知道如何让它以我想要的方式运作?

这是我的活动文件来源:

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;
    }
}

谢谢!

1 个答案:

答案 0 :(得分: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)

应该这样做。