Android - 进度对话框退出时的操作

发布于 2024-12-13 01:59:33 字数 2398 浏览 0 评论 0原文

我正在尝试为我的手机编写一个非常基本的应用程序,只是为了让自己重新回到编程的状态。我已经有了我想做的事情的框架和基础知识,我只需要在一个让我痛苦的小部分上得到帮助。

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

有谁知道如何让它按照我想要的方式工作?

这是我的活动文件来源:

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

谢谢!

I'm trying to write a pretty basic app for my phone, just to get myself back into the swing of programming. I've got the framework and the basics of what I want to do, I just need help on a small section that's killing me.

On my app, I have a button. When that button is pressed I want the app to change a piece of text to "Deciding...", pop up an indeterminate progress bar for a second or two then exit, then print the result of another activity, but only after the dialog has exited. I'm using a thread at the moment, but I can't get it to function correctly.

Does anyone have any idea how to get it to work the way I want it to?

Here's my Activity file source:

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2024-12-20 01:59:33

使用异步任务。

示例:

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

更新:

首先,您必须像这样声明该类:

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

I不知道 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)

应该可以做到。

Use AsyncTask.

An example:

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

Update:

First of all, you must declare the class like this:

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

I don't know how showDialog and removeDialog works, but I would do it like this instead:

Show dialog (in onPreExecute):

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

Remove/dismiss dialog (in onPostExecute):

progressDialog.dismiss();

Declaring each of the three methods:

protected Boolean doInBackground(Void... params)

protected void onPreExecute()

protected void onPostExecute(Boolean result)

That should do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文